0

I need to draw a system overlay view from a service. This is the part that adds this view to WindowManager:

WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

It failed with permission denied exception:

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

I have tried everything I found online and still not able to get this permission to pass.

  • I have SYSTEM_ALERT_WINDOW in manifest.
  • I have to use TYPE_APPLICATION_OVERLAY and not any other TYPE.
  • I checked Settings.canDrawOverlays and it is true.
  • I checked my app has Draw over other apps permission enabled in App Settings.

I looked at all the answers in Android: permission denied for window type 2038 using TYPE_APPLICATION_OVERLAY but none of them solve my problem.

Any help is appreciated! Thanks!

HeyThere
  • 503
  • 1
  • 5
  • 19
  • I'm facing this error on some devices, most of them are Vivo android 8.0. Have you already had the solution for this one? Plz let me know. Thanks – Huy Duong Tu Nov 06 '18 at 02:21

1 Answers1

3

You should choose another type for Android N:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
    flags = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}

WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        flags,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
shmakova
  • 6,076
  • 3
  • 28
  • 44