title: Permission denied for window type 2002 in Android Studio
body: <p>Today I installed latest version of Android Studio</p>
  
  <p>I am learning Floating Widgets in Android</p>
  
  <p>I started with applying this example</p>
  
  <p><a href="https://www.spaceotechnologies.com/android-floating-widget-tutorial/" rel="noreferrer">https://www.spaceotechnologies.com/android-floating-widget-tutorial/</a></p>
  
  <p>it compiles ok</p>
  
  <p>but when I run it in the emulator it crashes </p>
  
  <p>giving me this error</p>
  
  <pre><code>08-28 22:52:02.932 7400-7400/com.asmgx.MyApp.MyApp E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.asmgx.MyApp.MyApp, PID: 7400
      java.lang.RuntimeException: Unable to create service com.asmgx.MyApp.MyApp.FloatWidgetService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
          at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
          at android.app.ActivityThread.access$1300(ActivityThread.java:199)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:6669)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
       Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
          at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
          at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
          at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
          at com.asmgx.MyApp.MyApp.FloatWidgetService.onCreate(FloatWidgetService.java:36)
          at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
          at android.app.ActivityThread.access$1300(ActivityThread.java:199) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:193) 
          at android.app.ActivityThread.main(ActivityThread.java:6669) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
  </code></pre>
  
  <p>I tried resolving the issue and found this link</p>
  
  <p><a href="https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t">Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type</a></p>
  
  <p>they suggested adding this line to the manifest, which already added</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/&gt;
  </code></pre>
  
  <p>anyone know why am i getting this?</p>
  
  <p>PS. I used emulator with Android 28 and another with android 27</p>