赞
踩
进程保活时 , 遇到的问题 , 手机 Pixel 2 , Android 10.0 系统 ;
2021-04-08 18:17:09.953 15585-15585/kim.hsl.keep_progress_alive E/AndroidRuntime: FATAL EXCEPTION: main Process: kim.hsl.keep_progress_alive, PID: 15585 java.lang.RuntimeException: Unable to create service kim.hsl.keep_progress_alive.foreground_service.ForegroundService: java.lang.SecurityException: Permission Denial: startForeground from pid=15585, uid=10361 requires android.permission.FOREGROUND_SERVICE at android.app.ActivityThread.handleCreateService(ActivityThread.java:3582) at android.app.ActivityThread.access$1300(ActivityThread.java:200) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) 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: java.lang.SecurityException: Permission Denial: startForeground from pid=15585, uid=10361 requires android.permission.FOREGROUND_SERVICE at android.os.Parcel.createException(Parcel.java:1950) at android.os.Parcel.readException(Parcel.java:1918) at android.os.Parcel.readException(Parcel.java:1868) at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5202) at android.app.Service.startForeground(Service.java:695) at kim.hsl.keep_progress_alive.foreground_service.ForegroundService.onCreate(ForegroundService.java:18) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3570) at android.app.ActivityThread.access$1300(ActivityThread.java:200) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) 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.os.RemoteException: Remote stack trace: at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:9236) at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1189) at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:870) at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:20535) at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:977)
Android 9.0 后需要配置 android.permission.FOREGROUND_SERVICE 权限 , 不需要弹出对话框申请 , 只需要在 AndroidManifest.xml 中注册即可 ;
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kim.hsl.keep_progress_alive"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Keep_Progress_Alive"> </application> </manifest>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。