10

My app was working fine in Android version upto 27. But I found this exception in Android 9.0 Pie, API 28.

This is the trace.

java.lang.RuntimeException: Unable to create service com.example.demo: java.lang.SecurityException: Permission Denial: startForeground from pid=21535, uid=10088 requires android.permission.FOREGROUND_SERVICE
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
        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)

1 Answers1

25

From Docs

You need to add this permission in your AndroidManifest.xml file and you don't have to handle any permission check. It will always be granted.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  • 2
    That fixed the problem, thanks. Google doesn't care about backward compatibility at all. In this case they could've just asked a user for permission, instead of crashing app. – Oleg Gryb May 26 '19 at 16:27
  • 1
    Oh yes, they do care. If they didn't, they wouldn't allow a user to run your old at all. But when you, as a programmer, are rebuilding and targeting a new platform, it is your responsibility to clean up your code so that YOUR code is compatible with new requirements. – deLock Jul 05 '19 at 13:26
  • Two thumbs up, one for the answer and one for providing a to the answer in android doc. – Shawn Jan 04 '20 at 19:13