As of Android API 26, the Android system requires that a Service that "performs some operation that is noticeable to the user" register itself as a foreground service. And "foreground services must display a Notification."
I have a Service that plays audio files. When playback is paused, the user should be able to swipe the notification away (as with the Google Play Music app). But if I call stopForeground() then my Service is killed by the system within a few minutes.
How can I allow the notification to be swiped away when playback is paused?
But if I call stopForeground() then my Service is killed by the system within a few minutes
On Android 8.0+, it will be stopped after a minute.
How can I allow the notification to be swiped away when playback is paused?
Step #0: Save any data that you really need (e.g., track and playback position) to disk
Step #1: Call stopForeground()
Step #2: Raise a fresh notification, one without the "ongoing" flag set (i.e., setOngoing(false), which is the default), to reflect your paused state
Step #3: Ideally, call stopSelf(), to get the service out of memory and reduce your process importance to a normal level, rather than waiting the minute for Android to stop your service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With