Java Android 系统叠加窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32652533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:54:28  来源:igfitidea点击:

Android System overlay window

javaandroidandroid-permissions

提问by Tom

I'm trying to create a system overlay. But I keep getting "permission denied". I'm using SDK version 23.

我正在尝试创建一个系统覆盖。但我不断收到“权限被拒绝”。我正在使用 SDK 版本 23。

My manifest file:

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test" >

<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

The code I use to create the overlay:

我用来创建覆盖的代码:

    //mView = new HUDView(this);
    mButton = new Button(this);
    mButton.setText("Overlay button");
    mButton.setOnTouchListener(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.RIGHT | Gravity.TOP;
    params.setTitle("Load Average");
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mButton, params);

采纳答案by CommonsWare

First, there is no permission named SYSTEM_OVERLAY_WINDOW. It is SYSTEM_ALERT_WINDOW.

首先,没有名为 的权限SYSTEM_OVERLAY_WINDOW。它是SYSTEM_ALERT_WINDOW

Second, if your targetSdkVersionis 23 or higher, and you are running on Android 6.0+ devices, your app will not get this permission at the outset. Call Settings.canDrawOverlays()to see if you have the permission, and use ACTION_MANAGE_OVERLAY_PERMISSIONto lead the user over to Settings if you do not.

其次,如果您的targetSdkVersion年龄为 23 岁或更高,并且您在 Android 6.0+ 设备上运行,则您的应用一开始就不会获得此权限。调用Settings.canDrawOverlays()以查看您是否有权限,如果没有,则用于ACTION_MANAGE_OVERLAY_PERMISSION将用户引导至“设置”。

回答by Anthone

In AndroidManifest (for version < 23)

在 AndroidManifest 中(版本 < 23)

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>


public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 5469;
//Random value

    public void testPermission() {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
        }
    }

Result :

结果 :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
        if (Settings.canDrawOverlays(this)) {
            // You have permission
        }
    }
}

回答by Nonos

You can also lead the user over to the specific app's overlaying page. The documentation states:

您还可以将用户引导至特定应用的覆盖页面。该文件指出

Input: Optionally, the Intent's data URI can specify the application package name to directly invoke the management GUI specific to the package name. For example "package:com.my.app".

Input:可选地,Intent 的数据 URI 可以指定应用程序包名称以直接调用特定于包名称的管理 GUI。例如“包:com.my.app”。

So something like:

所以像:

intent.setData(Uri.fromParts("package", getPackageName(), null));

回答by Arkady

As an alternative solution, you can try WindowManager.LayoutParams.TYPE_TOAST instead of WindowManager.LayoutParams.TYPE_SYSTEM_ALERT. This not requires any permission. I'm using it to show image, maybe button will work to

作为替代解决方案,您可以尝试使用 WindowManager.LayoutParams.TYPE_TOAST 而不是 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT。这不需要任何许可。我用它来显示图像,也许按钮可以工作

回答by arwTheDev

for users below and above android 8 use

适用于 android 8 以下和以上的用户使用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
    LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}