2

Somehow I am having a hard time parsing the official description of the START_STICKY flag:

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

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.

Specifically, the following four parts do not make technical sense (aka make me go WTF):

  • "if this service's process is killed ..., then leave it in the started state" [how do you leave something that is killed in the started state?]
  • "Later the system will try to re-create the service. Because it is in the started state, ..." [why is it in the started state if the system is trying to re-create the service?]
  • "Because it is in the started state, it will guarantee to call onStartCommand(...) ..." ["it will guarantee to call"? sorry, can't parse that phrase linguistically]
  • "This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, ..." ["and stopped to run"??]

Does anybody have a better spec of this flag? And, to any Googlers reading this: wtf? Can you get it fixed internally pronto?

Onik
  • 19,396
  • 14
  • 68
  • 91
necromancer
  • 23,916
  • 22
  • 68
  • 115

2 Answers2

1

START_STICKY and START_NOT_STICKY

START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent

Community
  • 1
  • 1
mianlaoshu
  • 2,342
  • 3
  • 27
  • 48
1

As you know, there are two forms a service can have in Android:

The key point to understanding the description is that a Started service must manage its own lifecycle, i.e. the ONLY components that can stop the service is the service itself by calling stopSelf() or another component by calling stopService(). Once a Started service has been started (onStartCommand() returns) it’s in a started state and it won't stop unless either stopSelf() or stopService() is called. So, if the system prematurely kills the service (neither stopSelf() or stopService() has been called), the service still is considered to be in a started state. And it’s up to you to tell (signal to) the system how to proceed with the service after killing it by returning a flag in onStartCommand().

Probably the description of the START_STICKY flag given at the end of Extending the Service class will be more clear for you.

P.S. Regarding the "and stopped to run??" confusion, try reading it as "...explicitly started and stopped in order to run..."

EDIT:

Also take a look at this question.

Community
  • 1
  • 1
Onik
  • 19,396
  • 14
  • 68
  • 91