android - 应用程序覆盖活动
问题描述
我正在尝试创建一个在其他应用程序之上只有一个按钮的应用程序。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!Settings.canDrawOverlays(this)) {
Intent intent;
intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, RESULT);
}
manager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
200,
200,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.START;
params.x = 0;
params.y = 0;
rootView = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.activity_main, null);
manager.addView(rootView, params);
}
布局
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/sound_btn"
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
解决方案
推荐阅读
- java - 从 JFrame1 移动并从 Jframe 2 返回时恢复 Jframe1 的状态
- java - 如何将 PDF 文档页面大小设置为 3 x 5 英寸 (itextpdf)
- vba - 无法过滤掉 0
- javascript - 在 @param 和 @returns 中为函数使用相同的泛型类型
- xcode - Xcode 可以显示属性设置器的调用层次结构吗?
- spring-data-mongodb - MongoTemplate vs MongoOperations 使用哪一个
- php - 使用 Faker 伪造图像总是返回 false
- mysql - Ruby on Rails:两个模型都有 Has/Many 和 Belongs_to
- tesseract - Tesseract 不在另一台 PC 上工作
- node.js - 用 sqlite3 续集不会创建数据库