权限被拒绝:startForeground 需要 android.permission.FOREGROUND_SERVICE

Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

最近我们突然看到了以下一些堆栈跟踪。为什么会这样?这是从应用程序尝试通过媒体通知和所有内容将音频评论服务移动到前台时开始的。

java.lang.SecurityException: Permission Denial: startForeground from pid=1824, uid=10479 requires android.permission.FOREGROUND_SERVICE
    at android.os.Parcel.createException(Parcel.java:1942)
    at android.os.Parcel.readException(Parcel.java:1910)
    at android.os.Parcel.readException(Parcel.java:1860)
    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5198)
    at android.app.Service.startForeground(Service.java:695)
    at com.example.app.services.AudioService.setUpMediaNotification(AudioService.java:372)
    at com.example.app.services.AudioService.setUpAndStartAudioFeed(AudioService.java:328)
    at com.example.app.services.AudioService.onStartCommand(AudioService.java:228)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3667)
    at android.app.ActivityThread.access00(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
    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.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:9186)
    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:20434)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:976)

如果您设置了 targetSdkVersion = 28 (Android 9 / Pie) 或更高版本并且没有声明使用 FOREGROUND_SERVICE 权限,就会发生这种情况。

the migration notes 到 Android 9:

Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.

解决办法就是在AndroidManifest.xml中加入如下内容:

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

Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

面向 Android 9(API 级别 28)或更高级别并使用前台服务的应用程序必须请求 FOREGROUND_SERVICE permission

所以现在我们需要在清单文件中添加Foreground service permission

  • 它允许常规应用程序使用 Service.startForeground

样本

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

FOREGROUND_SERVICE is a normal permission, so the system automatically grants it to the requesting app.

Check this the migration notes of Android 9 / Pie

  • 改变

Foreground service permission

  • 总结

Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.

另读startForeground()

  • 面向 API Build.VERSION_CODES.P 或更高版本的应用必须请求权限 Manifest.permission.FOREGROUND_SERVICE 才能使用此 API.

对于 API 28 级或更高级别,需要 FOREGROUND_SERVICE 许可。否则,它不能运行并得到异常。

添加

即可解决
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

在 AndroidManifest.xml 文件中。

从 2019 年 11 月 1 日起,对于 Play 商店中的应用更新,至少 28targetSdkVersion 将是强制性的。因此您需要更改目标 API,然后请求权限 FOREGROUND_SERVICE 以避免 startForeground()

崩溃

注意 FOREGROUND_SERVICE 不需要运行时权限要求。仅将以下内容添加到 Manifests

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

上面这行应该加在<application

之前