9

I ran into a problem when trying to add content to WindowManager. What's the most interesting, if you compile through another compiler (AIDE), then everything works fine. I'm using the Android studio now. Here is the code below:

params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP|Gravity.LEFT;
        params.x = 0;
        params.y = y - barHeight;

        windowManager.addView(nb, params); // error there

AndroidManifest:

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

Error message:

java.lang.RuntimeException: Unable to create service priv.congard.ozonehd4k.controlpanel.NavigationBarService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@a8744a7 -- permission denied for window type 2003
                                                                                         at android.app.ActivityThread.handleCreateService(ActivityThread.java:3226)
                                                                                         at android.app.ActivityThread.-wrap5(ActivityThread.java)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                                                         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@a8744a7 -- permission denied for window type 2003
                                                                                         at android.view.ViewRootImpl.setView(ViewRootImpl.java:703)
                                                                                         at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                                                                                         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
                                                                                         at priv.congard.ozonehd4k.controlpanel.NavigationBarService.onCreate(NavigationBarService.java:64)

Unlike another question, in my case the window is not created not on the lock screen. With what this strange problem can be connected?

congard
  • 945
  • 2
  • 10
  • 28
  • Its not a duplicate. This is happening without a lock screen and I have the user give overlay popup permission. Why you mark as duplicate and dont reference the duplicates is beyond me – Brian Reinhold Jun 20 '18 at 18:53
  • 6
    I had same issue. I resolved it by having the window manager flag be TYPE_APPLICATION_OVERLAY is the SDK>=26 and TYPE_SYSTEM_OVERLAY for SDK<26 – Ziad Halabi Oct 26 '18 at 18:39

0 Answers0