Android: permission denied for window type 2038 using TYPE_APPLICATION_OVERLAY

Derwrecked picture Derwrecked · Sep 14, 2017 · Viewed 30.3k times · Source

I trying to create a view that is above other applications:

WindowManager.LayoutParams paramsDirectorView = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);

I have looked in other responses and found the following things for "drawing over applications":

  • I have android.permission.SYSTEM_ALERT_WINDOW in the manifest
  • I am doing the Settings.canDrawOverlays(this) check which comes back true.
  • I have done everything located here permission denied for window type

I am still getting "-- permission denied for window type 2038" error. As of now I am using TYPE_PHONE and it works, but it is deprecated and says to use TYPE_APPLICATION_OVERLAY. Can some one follow up on this as the TYPE_PHONE answer is not truly a resolution but a "patch work" solution that is deprecated in Android O.

I am running on Android 7.1.2

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1f47e89 -- permission denied for window type 2038 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3344) at android.app.ActivityThread.-wrap21(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1f47e89 -- permission denied for window type 2038 at android.view.ViewRootImpl.setView(ViewRootImpl.java:703) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at HeadService.TwoViewManager.(TwoViewManager.java:99) at HeadService.UIHeadService.onStartCommand(UIHeadService.java:65) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3327) at android.app.ActivityThread.-wrap21(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1583) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Answer

Cédric Dufouil picture Cédric Dufouil · Nov 3, 2017

I had the exactly same issue. I guess you should differentiate the target (before and after Oreo)

int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}

params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        LAYOUT_FLAG,
        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);