今天我安装了最新版本的Android Studio
我正在学习Android中的浮动小部件
我开始尝试应用这个例子
https://www.spaceotechnologies.com/android-floating-widget-tutorial/
它编译没有问题
但是当我在模拟器中运行它时,它崩溃了
并给了我这个错误
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)
我尝试解决这个问题并找到了这个链接
无法添加窗口android.view.ViewRoot$W@44da9bc0 -- 拒绝此窗口类型的权限
他们建议将这行添加到清单中,但已经添加了
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
有人知道我为什么会收到这个消息吗?
顺便说一句,我用的模拟器一个是安卓28的,另一个是安卓27的。
TYPE_APPLICATION_OVERLAY
已在 API 26 中添加,而非 22。https://developer.android.com/reference/android/view/WindowManager.LayoutParams - Behrouz.MTYPE_APPLICATION_OVERLAY
给了我window type 2038 的权限被拒绝
。清单文件中已经请求了android.permission.SYSTEM_ALERT_WINDOW
。 - user1801605