Stop Android service when application is closed using task manager

I am working on a foreground-enabled application that plays audio for a long period of time.

I do not want the Android OS to kill my service while running in the background, however I want to stop the service if the application is closed manually from the task manager.

The current behavior of my application is that services continue to run when the application closes in the task manager.

I noticed that Spotify achieves the desired effect, how is this done?

+7
android service
source share
1 answer

I think you can call stopSelf() from onTaskRemoved() . There is also a lot of undocumented android:stopWithTask , which, apparently, can have a <service> element to automatically stop the service when a task is stopped from the list of recent tasks.

Please note that I have not tried any of them, therefore YMMV.

+13
source share

All Articles