所以这就是问题所在,我正在尝试制作一个显示来自主应用程序的数据的叠加层,应用程序正在运行而没有错误,但实际上并没有显示数据。
Main.java:这是主应用程序,所有数据输入和其他内容都会发生。
package com.schnetzapps.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
public class Main extends Activity {
Switch toggleOverlaySwitch;
TextView overlayLabel;
EditText testText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.overlay, null);
overlayLabel = (TextView) vi.findViewById(R.id.overlayLabel);
toggleOverlaySwitch = (Switch) findViewById(R.id.openOverlay);
toggleOverlaySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggleService();
testText = (EditText) findViewById(R.id.testText);
overlayLabel.setText(testText.getText());
testText.setText(overlayLabel.getText());
}
});
}
private void toggleService(){
Intent intent = new Intent(this, OverlayService.class);
if(!stopService(intent)){
startService(intent);
}
}
}
Overlay.java:这是一个叠加层,旨在显示用户信息,因为无论其他应用程序可能打开,它都会发生变化。 包com.schnetzapps.myapplication;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class Overlay extends Service {
LinearLayout oView;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
oView = new LinearLayout(this);
int col = Color.parseColor("#4286f4");
oView.setBackgroundColor(col);
int width = 500;
int height = 500;
int x = 0;
int y = 0;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height, x, y,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.overlay, null);
oView.addView(layout);
wm.addView(oView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if(oView!=null){
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(oView);
}
}
}
activity_main.xml:这是主应用程序的设计。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:text="Open Overlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/openOverlay" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Test"
android:ems="10"
android:id="@+id/testText" />
</LinearLayout>
overlay.xml:这是我正在尝试设置文本的标签的叠加设计。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/overlayLayout" >
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/overlayLabel" />
</RelativeLayout>
</LinearLayout>
我没有得到任何空引用异常,指出我指向错误的TextView,并且叠加层根据需要切换很好,为什么叠加层上的文本不会更改为已输入EditText字段的内容?< / p>
已切换叠加层,但TextView未显示EditText中的文本:
我确信我错过了一些愚蠢的东西......非常感谢任何输入。
答案 0 :(得分:0)
经过一番摆弄后,我发现解决方案当然是Intent系统:intent.putExtra(key, value);
和intent.getStringExtra(key);
在上面的代码中,我更改了以下代码:
private void toggleService(){
Intent intent = new Intent(this, OverlayService.class);
if(!stopService(intent)){
startService(intent);
}
}
包含Intent推杆:
private void toggleService(String key, String value){
Intent intent = new Intent(this, OverlayService.class);
intent.putExtra(key, value);
if(!stopService(intent)){
startService(intent);
}
}
在Overlay Service中,我更改了大部分代码,现在看起来像这样:
package com.schnetzapps.myapplication;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class OverlayService extends Service {
String data;
LinearLayout oView;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand (Intent intent, int flags, int startId) {
data = intent.getStringExtra("testText");
RunOverlay();
return START_STICKY;
}
void RunOverlay () {
oView = new LinearLayout(this);
int col = Color.parseColor("#4286f4");
oView.setBackgroundColor(col);
int width = 500;
int height = 500;
int x = 0;
int y = 0;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(width, height, x, y,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.overlay, null);
oView.addView(layout);
TextView t = (TextView)oView.findViewById(R.id.overlayLabel);
t.setText(data);
wm.addView(oView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if(oView!=null){
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(oView);
}
}
}
结果当然是当切换更改时,叠加窗口将显示EditText字段中的文本。