title: Permission denied for window type 2002 & 2038
body: <p>I'm showing a service overly window and it's working properly with most of my users, however I find these errors on Crashlytics</p>
  
  <p>Older devices (Android 7 and below)</p>
  
  <pre><code>Fatal Exception: java.lang.RuntimeException: Unable to create service com.app.name.mainService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@43108e0 -- permission denied for window type 2002
     at android.app.ActivityThread.handleCreateService + 3172(ActivityThread.java:3172)
     at android.app.ActivityThread.-wrap5(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage + 1550(ActivityThread.java:1550)
     at android.os.Handler.dispatchMessage + 102(Handler.java:102)
     at android.os.Looper.loop + 154(Looper.java:154)
     at android.app.ActivityThread.main + 6077(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Method.java)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 865(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main + 755(ZygoteInit.java:755)
  </code></pre>
  
  <p>Newer devices (Especially Galaxy S8 Android 9.0)</p>
  
  <pre><code>Fatal Exception: java.lang.RuntimeException: Unable to create service com.app.name.mainService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@df3ca8f -- permission denied for window type 2038
     at android.app.ActivityThread.handleCreateService + 3768(ActivityThread.java:3768)
     at android.app.ActivityThread.access$1400 + 237(ActivityThread.java:237)
     at android.app.ActivityThread$H.handleMessage + 1801(ActivityThread.java:1801)
     at android.os.Handler.dispatchMessage + 106(Handler.java:106)
     at android.os.Looper.loop + 214(Looper.java:214)
     at android.app.ActivityThread.main + 7050(ActivityThread.java:7050)
     at java.lang.reflect.Method.invoke(Method.java)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493(RuntimeInit.java:493)
     at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)
  </code></pre>
  
  <p>My code I'm using is</p>
  
  <pre><code>if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.O) {
              params = new WindowManager.LayoutParams(
                      WindowManager.LayoutParams.MATCH_PARENT,
                      WindowManager.LayoutParams.MATCH_PARENT,
                      WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
  
                      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                              WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                              WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
                              WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                      PixelFormat.TRANSLUCENT);
          } else {
              params = new WindowManager.LayoutParams(
                      WindowManager.LayoutParams.MATCH_PARENT,
                      WindowManager.LayoutParams.MATCH_PARENT,
                      WindowManager.LayoutParams.TYPE_PHONE,
  
                      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                              WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                              WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
                              WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                      PixelFormat.TRANSLUCENT);
          }
  
          mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
          if (mWindowManager != null) {
              mWindowManager.addView(serviceRootView, params);
          }
  </code></pre>
  
  <p>I'm also making sure that Draw permission is granted and manifest file has permission for</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  </code></pre>
  
  <p>And I searched a lot on other questions, all suggested the above solution but it is not helpful as the crash still occurs!</p>