现象
桌面悬浮框在6.0上会因为SYSTEM_ALERT_WINDOW权限的问题,无法在最上层显示。
问题原因
SYSTEM_ALERT_WINDOW and WRITE_SETTINGS, 这两个权限比较特殊,不能通过代码申请方式获取,必须得用户打开软件设置页手动打开,才能授权。
路径是:Settings->Apps->App Setting->Draw over other apps . 然后手动打开应用的此权限。Manifest申请该权限是无效的。
注意:
- 预置应用应该是可以默认使用该权限的(经验说:预置应用默认开启所需要的权限,就算在apps->permission中显示的权限默认是关闭的)。
- 通过Google Play Store(Version 6.05 or heigher is required)下载的需要该权限的应用,会被自动授予该权限
参考如下:
It is a new behaviour introduced in Marshmallow 6.0.1.
Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically.
If instead the app is sideloaded, the permission is not automatically granted. You can try to download and install the Evernote APK from apkmirror.com. As you can see you need to manually grant the permission in Settings -> Apps -> Draw over other apps.
These are the commits [1] [2] that allow the Play Store to give the automatic grant of the SYSTEM_ALERT_WINDOW permission.
From: SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23
以及源码中对该部分的修改说明。注:在MTK 平台上目前未合入此条记录
规避方法
官方建议需要申请该权限时引导用户跳转到Setting中自己去开启权限开关
//参考自http://stackoverflow.com/questions/32061934/permission-from-manifest-doesnt-work-in-android-6
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
@TargetApi(Build.VERSION_CODES.M)
public void requestDrawOverLays() {
if (!Settings.canDrawOverlays(MainActivity.this)) {
Toast.makeText(this, "can not DrawOverlays", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + MainActivity.this.getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
} else {
// Already hold the SYSTEM_ALERT_WINDOW permission, do addview or something.
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
Toast.makeText(this, "Permission Denieddd by user.Please Check it in Settings", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Allowed", Toast.LENGTH_SHORT).show();
// Already hold the SYSTEM_ALERT_WINDOW permission, do addview or something.
}
}
}