5

A Foreground Service has a very high priority and is very unlikely to be killed by the system, so is there any point in returning START_STICKY in onStartCommand?

Edit: This question is not about the difference between START_STICKY and START_NON_STICKY, it's about the usability of START_STICKY in relation to Foreground Services.

0

1 Answer 1

5

"is very unlikely to be killed" != "will never be killed". For example, the user can terminate your process through a variety of means.

If you care about those scenarios and want to have your service be restarted (when eligible), use START_STICKY or START_REDELIVER_INTENT. If you are happy to let the service stay dead, use START_NOT_STICKY.

For example, if you are writing a music player, return START_NOT_STICKY, as it may not be appropriate for your app to start playing music at some arbitrary time later when the system elects to restart your service.

11
  • Oddly though the START_STICKY docs state: "This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback."
    – Elletlar
    Commented May 8, 2018 at 13:20
  • 1
    @Elletlar: I have never agreed with that recommendation from Google. I think I even filed an issue on it, but documentation issues tend to get routed to /dev/null in the Googleplex. Commented May 8, 2018 at 13:23
  • I guess the recommendation would make sense if the service is restarted very soon after it stops, but there is no guarantee of that.
    – Elletlar
    Commented May 8, 2018 at 13:25
  • Can you give an example for when the system will kill a foreground service that is playing music? Commented May 8, 2018 at 13:40
  • @FlorianWalther: If the user presses Force Stop in Settings, Android will terminate the process. Apparently, in some situations, removing the task from the overview screen will terminate a process despite a foreground service. Users might have a third-party "task manager" that terminates the process. And, due to old age, Android might terminate the process as part of the ordinary out-of-memory-killer logic. A foreground service does not live forever. Commented May 8, 2018 at 14:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.