在 Android 6.0 及更高版本上授予 SYSTEM ALERT WINDOW 权限
从 android 6.0 开始,此权限需要动态授予,
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
在 6.0 以下投掷权限被拒绝错误,
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@86fb55b -- permission denied for this window type
解决方案: -
请求覆盖权限如下,
if(!Settings.canDrawOverlays(this)){
// ask for setting
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);
}
检查结果,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OVERLAY_PERMISSION) {
if (Settings.canDrawOverlays(this)) {
// permission granted...
}else{
// permission not granted...
}
}
}