ForegroundServiceStartNotAllowedException even when app transitions from a user-visible state (Activity)

Viewed 85

I need to start a service to allow background audio playing when the user backgrounds the app:

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

This is fired in an Activity as the app is backgrounded. I'm detecting backgrounding apps by registering a LifecycleObserver and using this method:

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppBackgrounded() {
    // notify listeners
}

The background notify is working correctly, but I'm still getting the exception, even though:

Your app transitions from a user-visible state, such as an activity.

As described here https://developer.android.com/guide/components/foreground-services#background-start-restriction-exemptions

What exactly does "transitions from a user-visible state" mean, and am I using it correctly here? Unfortunately, I can't reproduce and it's only showing on Firebase.

0 Answers
Related