Search code examples
androidservicenotificationsmedia-playerandroid-ondestroy

Android OS killing service, notification is staying


My Android App plays audio. The app starts a service, the service starts a notification with media buttons.

When the OS is low on resources, the OS kills my service. Yet the notification, with the media buttons, is staying. Because the service is gone, the media buttons don't function anymore. A negative user experience.

I've been looking for solutions for a week. The OS is not informing me that the service is being killed. Please help.


Solution

  • You should use foreground service for that and add this code before service will be destroyed:

    @Override
    public void onDestroy() {
        stopForeground(true);
        super.onDestroy();
    }