Granting SYSTEM ALERT WINDOW Permission on android 6.0 and above
suggest changeFrom Android 6.0, the user grants this permission from Settings. It is not a runtime permission requested with requestPermissions.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
The following permission-denied error is thrown on Android 6.0:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@86fb55b -- permission denied for this window type
Solution:
Request the overlay permission as follows. REQUEST_OVERLAY_PERMISSION is any request code you choose.
private static final int REQUEST_OVERLAY_PERMISSION = 1;
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);
}
Check the result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_OVERLAY_PERMISSION) {
if (Settings.canDrawOverlays(this)) {
// permission granted...
}else{
// permission not granted...
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents