Today I installed latest version of Android Studio
I am learning Floating Widgets in Android
I started with applying this example
https://www.spaceotechnologies.com/android-floating-widget-tutorial/
it compiles ok
but when I run it in the emulator it crashes
giving me this error
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)
I tried resolving the issue and found this link
Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type
they suggested adding this line to the manifest, which already added
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
anyone know why am i getting this?
PS. I used emulator with Android 28 and another with android 27
This is occurring because the targetSdkVersion in the example and your targetSdkVersion are different. Use the flag TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE in WindowManager.LayoutParams:
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY