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

Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.access$1600(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权限,则会发生这种情况。

从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中添加以下内容:

1
2
3
4
5
6
7
<manifest ...>
     ...
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     ...
     
     ...
</manifest>


Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

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

所以现在我们需要在清单文件中添加Foreground服务权限

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

样品

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

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

检查一下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权限。否则,它将无法运行并出现异常。

这将通过添加解决

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

在AndroidManifest.xml文件中。


从2019年11月1日开始,Play商店中的应用程序更新必须至少具有28targetSdkVersion。因此,您需要更改目标API,然后请求权限FOREGROUND_SERVICE以避免startForeground()崩溃


注意FOREGROUND_SERVICE不需要运行时权限要求。仅在清单下面添加

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

以上行应添加在之前