Permission from manifest doesn't work in Android 6

AndroidAndroid 6.0-Marshmallow

Android Problem Overview


It completely ignores:

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

So I got exception:

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

It's not even listed:

enter image description here

How should I fix it? Thanks.

EDIT:

It's listed in Configure apps/ Advanced / Draw over other app. So i turn it on and now it works fine, but why there isn't any dialog to ask about permission when i run my app? All perrmissions was turned off by deafult and i need to go to settings and mannualy turn it on?

Android Solutions


Solution 1 - Android

Thanks to [CommonsWare's blog post][1], I got some clue.

Assuming your code is in Activity or Fragment, check the overlay permission and make a request for it if necessary:

public static int OVERLAY_PERMISSION_REQ_CODE = 1234;

public void someMethod() {
    if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}

Then, re-check the permission for better UX:

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

[1]: https://commonsware.com/blog/2015/08/17/random-musings-android-6p0-sdk.html "Random Musings on the Android 6.0 SDK"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDjordje TankosicView Question on Stackoverflow
Solution 1 - AndroidShigerelloView Answer on Stackoverflow