8

I am working on an app with a foreground service that plays audio for an extended period of time.

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

The current behavior of my app is that the services continues to run when the app is closed in the task manager.

I've noticed that Spotify achieves the desired effect, how is it accomplished?

Jacob Tabak
  • 8,044
  • 3
  • 24
  • 32

1 Answers1

21

I think that you can call stopSelf() from onTaskRemoved(). There's also a severely underdocumented android:stopWithTask you apparently can have on the <service> element to automatically stop the service when the task is stopped from the recent-tasks list.

Note that I haven't tried either of these, so YMMV.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks CommonsWare, this seems to work! Are you aware of any adb shell command that will simulate the OS destroying the activity? I would hate to accept the answer without knowing for sure that the service won't stop if the activity is killed by the OS. I tried using `adb shell am force-stop com.my.app.package` but that kills the service as well. – Jacob Tabak Aug 08 '14 at 16:36
  • @gnawatyourpaw: Sorry, I do not know a shell command that mimics swiping the app off the recent-tasks list, which is really what you are after. There may be such a command. I usually just swipe the app off the recent-tasks list. :-) – CommonsWare Aug 08 '14 at 16:44
  • 1
    CommonsWare, I was actually looking for a command to simulate the OS killing the app automatically, not by removing it from recents. But, I went to developer options -> don't keep activities, added a log message in onDestroy(), and determined that your answer is in fact correct. It only kills the service when the task is manually ended by the user, not when the OS destroys the activity. So, thanks again for your excellent answer. How did you happen to know this information off the top of your head if you've never used it? The documentation is so unclear. – Jacob Tabak Aug 08 '14 at 16:48
  • One last comment - android:stopWithTask is probably the better solution, you may want to reword your answer so other users tend toward that solution rather than the other. – Jacob Tabak Aug 08 '14 at 16:51
  • @gnawatyourpaw: "How did you happen to know this information off the top of your head if you've never used it?" -- my mind is like a steel trap (by which I mean that it's rusty, and when I use it, it squeaks a lot. :-) More seriously, I remember running across `onTaskRemoved()` recently, and the JavaDocs for there led me to `android:stopWithTask`. "android:stopWithTask is probably the better solution" -- yes, except for the severely-underdocumented part, which makes me somewhat nervous about it. – CommonsWare Aug 08 '14 at 16:57
  • 1
    This works for me (in particular, I had the opposite problem, and required stopWithTask="false"). As of March 2017, stopWithTask is not documented where it should be (https://developer.android.com/guide/topics/manifest/service-element.html), but has a passing reference here: (https://developer.android.com/reference/android/content/pm/ServiceInfo.html) – Brian Mar 07 '17 at 17:51