250

Lately we have suddenly been seeing a few of the following stack traces. Why could that be? This is from when the app tries to move an audio commentary service into the foreground with a media notification and everything.

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)
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76

5 Answers5

505

This will happen if you have set targetSdkVersion = 28 (Android 9 / Pie) or above and have not declared the usage of the FOREGROUND_SERVICE permission.

From the migration notes for 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.

The solution is to just add the following in AndroidManifest.xml:

<manifest ...>
     ...
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     ...
     <application ...>
     ...
</manifest>
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76
  • 8
    Does that mean if our `targetSdkVersion` is lower than 28, we don't need any permission to run the Foreground services in devices running 28? –  Sep 27 '18 at 07:39
  • @Volleyball your app won't install on devices running 28 if your targetSdkVersion is lower than 28 – Sjd Feb 19 '19 at 13:34
  • 2
    @Sjd They will install just fine. It's not like it's `maxSdkVersion`. – Roy Solberg Feb 19 '19 at 14:45
  • @Sjd Roy is right, the app will install without any issues, however we won't be able to make use of new things added in API 28. –  Feb 23 '19 at 14:05
  • 13
    Up-voting your answer, but release notes are not an excuse for building non-backward compatible API's. In this case Google could have asked a user to give permission or at least provide a hint instead of crashing app silently. Android becomes very difficult to maintain lately. – Oleg Gryb May 26 '19 at 16:34
  • In one app that has sdkVersion 28 and i developed it does not need to this permission and in other app that i developed it need to this permission ,its strang , @RoySolberg – milad salimi Jun 25 '19 at 07:14
  • 2
    @miladsalimi If you open AndroidManifest.xml in Android Studio you can choose to see the "merged manifest". Maybe there's a dependency that injects that permission usage for you? – Roy Solberg Jun 25 '19 at 10:35
  • "This is what you get for not reading the release notes properly." There is probably a more polite/friendly way to transport this message. – Patrick Oct 06 '19 at 18:56
  • @patrickf I agree, but notice how it's myself answering my own question. I was just punishing myself a bit for upgrading the app to Android 9 and not catching this before. :) – Roy Solberg Oct 07 '19 at 04:28
  • 1
    @RoySolberg ah of course I missed that :) Maybe it's time to be a little nicer to yourself, Im sure you are doing fine :) – Patrick Oct 07 '19 at 18:19
  • They will also RUN just fine on devices under 28 without the changes needed to run on 28 and up. However having the targetSdk at lower than 28 would have have them still run on 28 and up devices. Google should figure out a system for where it wouldn't need to require specific targets, but the system also has so many hacks that alter app behavior based on target sdk that it's hard for them to go back. – Lassi Kinnunen Feb 05 '20 at 15:08
  • work for me~ [android compile version 32 target version 32] –  Godrick Jul 28 '23 at 08:56
51

Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission.

So now we need to add Foreground service permission in manifest file

  • it Allows a regular application to use Service.startForeground

SAMPLE

<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

  • Change

Foreground service permission

  • Summary

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.

Also Read startForeground()

  • Apps targeting API Build.VERSION_CODES.P or later must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API.
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • sir i tried ur solution but its not working for me for all android devices.like in Oppo F11 Pro its working but in the Samsung,Nokia and Mi phone app is crashed and got same error in log.can u tell me what i do for fix? – Kapil Soni Feb 22 '20 at 15:03
  • @Kapilsoni ` got same error in log` what error you are getting – AskNilesh Feb 24 '20 at 04:26
  • i got this error " android.app.RemoteServiceException: Bad notification for startForeground: " – Kapil Soni Feb 24 '20 at 07:05
  • 1
    @Kapilsoni I know it's an old post, but have you tried to reinstall...? Sometimes reinstall fix the issue, like the importance change of the notification channel. – Samuel T. Chou Aug 13 '20 at 09:21
22

Noting that FOREGROUND_SERVICE doesn't require runtime permission requirement. only add below to Manifests

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

the above line should be added before <application

15

For API level 28 or higher, it requires FOREGROUND_SERVICE permission. Otherwise, it can not run and got an exception.

It will be solved by adding

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

in AndroidManifest.xml file.

Zaman
  • 191
  • 1
  • 3
6

A targetSdkVersion of at least 28 will be mandatory from Nov 1 2019 for app updates in the Play Store. So you'll need to change the target API, and then request the permission FOREGROUND_SERVICE to avoid the crash on startForeground()

Vaibhav Vishal
  • 6,576
  • 7
  • 27
  • 48
O Wigley
  • 157
  • 1
  • 4