Android Studio - 在叠加 Activity 中显示主要 Activity 的信息
Android Studio - Show Information From Main Activity in Overlay Activity
所以这就是问题所在,我正在尝试制作一个显示来自主应用程序的数据的叠加层,该应用程序 运行 没有错误,但它实际上没有显示数据。
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 字段中输入的内容?
截图
覆盖未切换为打开,因此覆盖不可见:
覆盖已打开,但 TextView 未显示来自 EditText 的文本:
我确定我遗漏了一些愚蠢的东西...非常感谢任何输入。
折腾了一番,发现解决办法当然是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);
}
}
在覆盖服务中,我更改了大部分代码,现在看起来像这样:
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);
}
}
}
结果当然是当切换开关改变时,叠加层 window 将显示来自 EditText 字段的文本。
所以这就是问题所在,我正在尝试制作一个显示来自主应用程序的数据的叠加层,该应用程序 运行 没有错误,但它实际上没有显示数据。
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 字段中输入的内容?
截图
覆盖未切换为打开,因此覆盖不可见:
覆盖已打开,但 TextView 未显示来自 EditText 的文本:
我确定我遗漏了一些愚蠢的东西...非常感谢任何输入。
折腾了一番,发现解决办法当然是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);
}
}
在覆盖服务中,我更改了大部分代码,现在看起来像这样:
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);
}
}
}
结果当然是当切换开关改变时,叠加层 window 将显示来自 EditText 字段的文本。