当前位置:   article > 正文

【错误记录】前台进程报错 ( Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE )_permission denial: startforeground from pid=4247,

permission denial: startforeground from pid=4247, uid=10131 requires android





一、报错信息



进程保活时 , 遇到的问题 , 手机 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

在这里插入图片描述





二、修改方案



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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号