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?

Ziem
  • 6,579
  • 8
  • 53
  • 86
Djordje Tankosic
  • 1,975
  • 2
  • 20
  • 21
  • 1
    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. – Shigerello Aug 18 '15 at 05:55
  • 2
    I raised a bug report here: https://code.google.com/p/android-developer-preview/issues/detail?id=2971 – Mark Aug 18 '15 at 14:17
  • 9
    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! – Christian García Aug 25 '15 at 15:51
  • Possible duplicate of [Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type](http://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t) – ceph3us Nov 13 '16 at 18:12

1 Answers1

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...
        }
    }
}
Shigerello
  • 1,884
  • 1
  • 14
  • 15
  • Thanks! It works, but it doesn't show normal (small) dialog, it shows full screen dialog when i request permission, you can see here: http://imgur.com/eWf5DSC Is this normal? – Djordje Tankosic Aug 18 '15 at 07:33
  • 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. – Shigerello Aug 18 '15 at 10:07
  • Your solution works for me. But the regular way which is provided in the android docs didn't work(http://developer.android.com/training/permissions/requesting.html) – Noundla Sandeep Oct 27 '15 at 11:47
  • This issue is mentioned as 'Wont Fix' here.(https://code.google.com/p/android-developer-preview/issues/detail?id=2971#c9) – Noundla Sandeep Oct 27 '15 at 12:02
  • 4
    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. – Djordje Tankosic Nov 07 '15 at 03:09
  • @Shigerello 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. – Dũng Trần Trung Nov 24 '15 at 02:02
  • 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. – user2498079 Dec 25 '15 at 17:18
  • 1
    How to add this permission when calling some overlay draw via **Android Service ?** example : ChatHead Draw – Elshan Jun 23 '16 at 04:35
  • @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) – Shigerello Jun 25 '16 at 01:33
  • To anyone having the same grayed out problem as @DjordjeTankosic had: make sure you have the line `` inside your manifest file. Also make sure it is NOT between your brackets! That solved the problem for me. I had it between my application brackets, and put it just before the last line with . – Julian Declercq Nov 03 '16 at 19:21