Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.SecurityException:权限拒绝:startForeground Android 9.0 API 28_Android_Android Service_Foreground Service - Fatal编程技术网

java.lang.SecurityException:权限拒绝:startForeground Android 9.0 API 28

java.lang.SecurityException:权限拒绝:startForeground Android 9.0 API 28,android,android-service,foreground-service,Android,Android Service,Foreground Service,我的应用程序在安卓版本(高达27)下运行良好。但我在Android 9.0 API 28中发现了这个例外 这是痕迹 java.lang.RuntimeException: Unable to create service com.example.demo: java.lang.SecurityException: Permission Denial: startForeground from pid=21535, uid=10088 requires android.permission.FOR

我的应用程序在安卓版本(高达27)下运行良好。但我在Android 9.0 API 28中发现了这个例外

这是痕迹

java.lang.RuntimeException: Unable to create service com.example.demo: java.lang.SecurityException: Permission Denial: startForeground from pid=21535, uid=10088 requires android.permission.FOREGROUND_SERVICE
        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)

您需要在
AndroidManifest.xml
文件中添加此权限,而不必进行任何权限检查。这将永远是理所当然的

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />


解决了问题,谢谢。谷歌根本不关心向后兼容性。在这种情况下,他们可以向用户请求许可,而不是让应用程序崩溃。哦,是的,他们真的在乎。如果他们不这样做,他们根本不允许用户运行你的旧系统。但是,当你作为一名程序员,正在重建并瞄准一个新的平台时,你有责任清理你的代码,使你的代码与新的需求兼容。竖起两个大拇指,一个用于答案,另一个用于在android doc中提供答案。