Permission from manifest doesn't work in Android 6
Asked Answered
M

1

67

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?

Millikan answered 18/8, 2015 at 0:50 Comment(4)
I got this problem too. I also tried to get the permission for SYSTEM_ALERT_WINDOW by calling Activity#requestPermissions(), but this doesn't work.Iyar
I raised a bug report here: code.google.com/p/android-developer-preview/issues/…Leg
By the way, the permission is listed under Settings -> Apps -> Gear icon in the top bar -> Draw over other apps (under the Advanced section). Very, very hidden!Continent
Possible duplicate of Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window typeEmmerich
I
127

Thanks to CommonsWare's blog post, 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...
        }
    }
}
Iyar answered 18/8, 2015 at 7:1 Comment(10)
Thanks! It works, but it doesn't show normal (small) dialog, it shows full screen dialog when i request permission, you can see here: imgur.com/eWf5DSC Is this normal?Millikan
Well… I'm not sure. As CommonsWare mentioning in its blog post, there is little (or no) documentation about changes about SYSTEM_ALERT_WINDOW. There can be a simpler approach, like with a small dialog asking the permission, but currently, this approach is the only one I know.Iyar
Your solution works for me. But the regular way which is provided in the android docs didn't work(developer.android.com/training/permissions/requesting.html)Neon
This issue is mentioned as 'Wont Fix' here.(code.google.com/p/android-developer-preview/issues/…)Neon
Yes, but this permission is broken. What will happen if the user disable this permission in settings, and some service (which needs this permission) is running? In this case it's not possible to notify the service to shutdown itself, beacuse there isn't context, so all i can do is to catch an exception.Millikan
@Iyar I tried this, successfully enabled the option but when I start the floating widget in my app, android studio showed that I didn't have SYSTEM_ALERT_WINDOW permission.Sadiesadira
This solution doesn't work on Android Wear and results in a crash citing settings activity missing. I even granted this permission to the app on phone side app.Thessalonians
How to add this permission when calling some overlay draw via Android Service ? example : ChatHead DrawHoneyed
@devopsEMK You may need to implements an activity that can be started via Context#startActivity or a fragment that can be managed inside a Service . Then, inside this activity or fragment, check the permission. If you want to receive the permission grant result from activity or fragment, you may need additional steps to implement an event callback mechanics (like binder-based IPC messaging or LocalBroadcastManager)Iyar
To anyone having the same grayed out problem as @DjordjeTankosic had: make sure you have the line <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> inside your manifest file. Also make sure it is NOT between your <application> </application> brackets! That solved the problem for me. I had it between my application brackets, and put it just before the last line with </manifest>.Dior

© 2022 - 2024 — McMap. All rights reserved.