android - Settings.ACTION_MANAGE_OVERLAY_PERMISSION 权限不适用于 API 级别 23 以上的所有设备

标签 android android-permissions

我正在测试我的应用程序在(Coolpad(Lolipop MRI)、Samsung Galaxy grand neo(Kitkat)、Redmi(Marshmallow)、Lenovo z2 plus(Marshmallow))等设备上的 Action 叠加层,以在来电屏幕上显示对话框。事情似乎适用于联想 z2 plus() 以外的设备。

直接正式请求许可我得到了异常(exception):

   public void testPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        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);
          }
    }
}

android.content.ActivityNotFoundException:未找到处理 Intent { act=android.settings.action.MANAGE_OVERLAY_PERMISSION 的 Activity

现在我将请求权限更改为:
 public void testPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
          Intent intent = new  Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
          Uri.parse("package:" + getPackageName()));
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }
    }
}

但它仍然无法在 zuk 2 中请求许可。感谢您的即时帮助。

最佳答案

ACTIVITY_MANAGE_OVERLAY_PERMISSION 已添加到 API 级别 23 .在发送 Intent 之前,您需要检查匹配的 Activity 是否可用。文档中明确指出,

Docs: In some cases, a matching Activity may not exist, so ensure you safeguard against this.



在 manifest.xml 中为 SYSTEM_ALERT_WINDOW 定义权限,

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

Intent

    public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 2323;

    public void checkPermission() {
        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方法,Intent不会返回任何数据到onActivityResult方法。最好在 onActivityResult 中再次检查覆盖权限。方法

@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
        }
    }
}

Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action Settings.ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().

关于android - Settings.ACTION_MANAGE_OVERLAY_PERMISSION 权限不适用于 API 级别 23 以上的所有设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44179407/

相关文章:

android - 如何在启用 LockTask 模式的 Android 应用程序中获得与 USB 闪存驱动器通信的运行时权限?

flutter - Flutter : PlatformException(no_available_camera,没有可用于拍照的相机。,null,null)

android - 联系人选择器过滤

android - 在使用 Android MediaPlayer 播放音频之前添加延迟

安卓 : Using context to get all TextView

android - 如果用户拒绝打开蓝牙,则退出 Android 应用程序

Android READ_PHONE_STATE 运行时权限要求调用和管理电话

java - 为什么我对位于/data/user_de/中的文件的权限被拒绝

android - 将 Bundle 保存到 SharedPreferences

java - 当后端更改需要更新的应用程序时如何强制更新应用程序