java - 系统覆盖窗口未按预期工作
问题描述
我正在开发一个可以在所有应用程序顶部绘制一个窗口的应用程序。基本上我想制作一个像夜间模式这样的应用程序。所以为此,我想开发一个可以在所有应用程序之上显示 TextView 的应用程序(只是为了测试我的想法)。我是 android 新手,这是我的第二个应用程序。在学习了 udacity android 基础课程后,我也在自学。
我有一个带有 TextView 的 MainActivity java 文件。当用户触摸它发送到从服务扩展的 OnScreen java 文件的文本视图时。到目前为止,我的应用程序运行良好。它进入 OnScreen 类并制作吐司。在这之后我有两个问题。
敬酒后(这意味着启动 OnScreen 服务类),我无法触摸任何其他应用程序。我使用了“FLAG_NOT_FOCUSABLE”。在开发人员指南中说它可以通过窗口传递触摸事件。但事实并非如此。
它不显示文本视图。实际上我不知道如何将它插入到 OnScreen 类中。
我的应用最低 SDK 版本是 17。
清单.xml 文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".OnScreen"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
</manifest>
主要活动
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void hello(View v){
Intent intent = new Intent(this, OnScreen.class);
startService(intent);
}
}
屏幕活动
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import static com.prabathhewa.eclips.R.id.dimmer;
public class OnScreen extends Service {
public OnScreen() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
TextView layout = inflater.inflate(R.layout.on_screen, (ViewGroup) findViewById());
View myView = new View(this);
WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
overlay.gravity = Gravity.TOP | Gravity.RIGHT;
WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, overlay);
}
}
我想显示的带有文本视图的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dimmer"
android:background="#7EFFEB3B"
tools:context=".OnScreen">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On screen"
android:textSize="40sp"
android:layout_gravity="center"
android:textColor="#FF0505"/>
</LinearLayout>
在问这个之前我读了
但我找不到我的问题的确切解决方案。
更新:
我设法通过将layourparams设置为解决了我的第一个问题
WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);
此外,它还帮了我一个忙,它也覆盖了通知栏。在这里,我使用了一个 xml 文件作为我的视图。由哪个定义
LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);
但我不知道如何进入 on_screen.xml 并获取文本视图。
解决方案
我设法通过将layourparams设置为解决了我的第一个问题
WindowManager.LayoutParams overlay = new WindowManager.LayoutParams(-1, -1, 2006, 280, -3);
此外,它还帮了我一个忙,它也覆盖了通知栏。在这里,我使用了一个 xml 文件作为我的视图。由哪个定义
LayoutInflater inflator=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View myView= (View) inflator.inflate(R.layout.on_screen, null);
但我不知道如何进入 on_screen.xml 并获取文本视图。
推荐阅读
- macos - 在 mac osx 和 Tiller 上安装 Helm 2.9
- git - Azure DevOps`不能变基超过 25 次提交`
- ios - 即使应用程序被杀死,iOS swift如何知道是否有任何联系人更新
- c - 我可以把这个要求变成一个宏吗?
- diff - 示例 vcdiff-java 客户端
- google-sheets - 编号对删除 Google 表格中的行不敏感
- r - Rstudio 是否将 '.Rproj.user' 和 '.Ruserdata' 放在 '.gitignore' 中?
- javascript - 使用 Jest 模拟函数中定义的 const
- javascript - 如何用 html 标签替换字符?(丰富的文字)
- ios - 如何在 React Native 中获取带有 UITableViewCellStyleSubtitle 的原生 UITableView?