I have an application that implements MediaBrowserServiceCompat
. When playing music, it runs in the foreground, with a media control notification that the system makes non-dismissible. When paused, it comes out of the foreground, and the notification is retained. Standard stuff.
When this service is automatically killed by the system, the notification is not removed.
You can simulate this by putting the app in the non-foreground paused state, leaving the app, and issuing this command:
adb shell am kill com.myapp.package
The notification remains. You get this Logcat message:
W/ActivityManager: Scheduling restart of crashed service com.myapp.package/com.myapp.package.playback.platform.AndroidMediaService
None of the obvious hooks (e.g. onDestroy
, onTaskRemoved
etc) on the service appear to be called - it seems to be outright killed. Because the service is started with START_NOT_STICKY
, we also don't get onCreate
called on a new instance either.
A partial solution to this is to make the service sticky, and cancel the notifications when the service is revived. However this can take significant time to actually happen, during which the notification doesn't work, so isn't ideal. It may also have further consequences.
Other applications seem to have no trouble with this.
Repeat this scenario (pause, leave app) and carry out the commands, for example on Google Play Music:
adb shell am kill com.google.android.music
or Spotify:
adb shell am kill com.spotify.music
and you find that their notifications disappear immediately as if they are cancelled on teardown.
What might they be doing to make this happen?