androidforeground-servicedevice-adminstartforegroundservice

Android 14: InvalidForegroundServiceTypeException when starting FGS with SYSTEM_EXEMPTED type


I’m seeing a repetitive crash in production when starting a foreground service from the background. The issue has occurred ~177K times in the last 30 days, mostly on Samsung devices (~50%) running Android 14 (~66%), with ~90% happening when the app is in the background.

The crash: android.app.InvalidForegroundServiceTypeException: Starting FGS with type none

I’m using ContextCompat.startForegroundService() to start the service:

ContextCompat.startForegroundService(context, Intent(context, MyService::class.java))

And inside MyService.onStartCommand():

ServiceCompat.startForeground(
    this,
    getNotificationId(),
    notification,
    ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED
)

The service is declared in the manifest with:

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="false"
    android:foregroundServiceType="systemExempted" />

Permissions declared:

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

App also uses DeviceAdmin, Exact alarms, and notification permissions.

Device Admin and Exact alarms permission makes my app systemExempted

I can’t reproduce this crash on physical Samsung devices or emulators. It seems like startForeground() is still considered as using type none, even though the service and call specify SYSTEM_EXEMPTED.

Is there something I’m missing about how Android 14 handles background FGS starts, especially on Samsung devices? Has anyone else faced this or found a workaround?

Complete crash logs :

 Fatal Exception: android.app.InvalidForegroundServiceTypeException: Starting FGS with type none callerApp=ProcessRecord{8d44878 6376:com.app.myapp/u0a328} targetSDK=34 has been prohibited
       at android.app.InvalidForegroundServiceTypeException$1.createFromParcel(InvalidForegroundServiceTypeException.java:53)
       at android.app.InvalidForegroundServiceTypeException$1.createFromParcel(InvalidForegroundServiceTypeException.java:49)
       at android.os.Parcel.readParcelableInternal(Parcel.java:5089)
       at android.os.Parcel.readParcelable(Parcel.java:5071)
       at android.os.Parcel.createExceptionOrNull(Parcel.java:3251)
       at android.os.Parcel.createException(Parcel.java:3240)
       at android.os.Parcel.readException(Parcel.java:3223)
       at android.os.Parcel.readException(Parcel.java:3165)
       at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7620)
       at android.app.Service.startForeground(Service.java:863)
       at androidx.work.impl.foreground.SystemForegroundService$Api31Impl.startForeground(SystemForegroundService.java:194)
       at androidx.work.impl.foreground.SystemForegroundService$1.run(SystemForegroundService.java:130)
       at android.os.Handler.handleCallback(Handler.java:959)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loopOnce(Looper.java:249)
       at android.os.Looper.loop(Looper.java:337)
       at android.app.ActivityThread.main(ActivityThread.java:9493)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:636)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1005)

I have been going through the official documentation but found nothing that makes sense to me, also I have been testing but unfortunately couldn't replicate it on any configuration on emulators or on real devices I have and I have been testing on Samsung : Note 10, and 25 Ultra, Pixel 8 and some other devices I could find.


Solution

  • As explained in the Support for long-running workers guide, if you are using WorkManager's support for long-running workers, as your stack trace indicates that you are using, then you need to add the foregroundServiceType to WorkManager's SystemForegroundService:

    <service
       android:name="androidx.work.impl.foreground.SystemForegroundService"
       android:foregroundServiceType="systemExempted"
       tools:node="merge" />