首页 > 解决方案 > 应用程序覆盖活动

问题描述

我正在尝试创建一个在其他应用程序之上只有一个按钮的应用程序。

@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" />

它有效,但在启动时我看到了活动。我应该怎么做才能在启动时只看到按钮?在此处输入图像描述

标签: androidbuttonoverlay

解决方案


推荐阅读