Android Granting SYSTEM_ALERT_WINDOW Permission on android 6.0 and above


From android 6.0 this permission needs to grant dynamically,

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

Throwing below permission denied error on 6.0,

Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@86fb55b -- permission denied for this window type

Solution :-

Requesting Overlay permission as below,

    // ask for setting 
     Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
     Uri.parse("package:" + getPackageName()));
     startActivityForResult(intent, REQUEST_OVERLAY_PERMISSION);

Check for the result,

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OVERLAY_PERMISSION) {
        if (Settings.canDrawOverlays(this)) {
            // permission granted...
            // permission not granted...