Meaning of onStartCommand () function return value in Android Service, androiddialog Return Value
OnStartCommand () is called by the Android system and essentially the onStart () method.
OnStartCommand () returns the following types of values:
1) START_STICKY
Explanation:
Constant to return fromonStartCommand
: If this service's process is killed while it is started (after returning fromonStartCommand
), 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 callonStartCommand
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 discovery Ming background music playback.
Note: When the returned value is START_STICKY, the service will only stop running when we want it to end running. Even if the process where the service is located is killed, the system creates a new service.
2) START_STICKY_COMPATIBILITY
Compatibility versionSTART_STICKY
That does not guarantee thatonStartCommand
Will be called again after being killed.
Note: compatible versions of START_STICKY are not guaranteed to be restarted after being killed.
3) START_NOT_STICKY
Constant to return fromonStartCommand
: If this service's process is killed while it is started (after returning fromonStartCommand
), And there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit callContext.startService(Intent)
. The service will not receiveonStartCommand(Intent, int, int)
Call with a null Intent because it will not be re-started if there are no pending Intents to deliver.
This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explain it start themselves again later to do more work. an example of such a service wocould be one that polls for data from a server: it cocould schedule an alarm to poll every N minutes by having the alarm start its service. when itsonStartCommand
Is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. if its process is killed while doing that check, the service will not be restarted until the alarm goes off.
Note: Unlike START_STICKY, kil does not restart a new service. It is applicable to tasks that can run with a wide margin of system resources (such as memory) and stop with a low margin of resources (such as insufficient memory.
4) START_REDELIVER_INTENT
Constant to return fromonStartCommand
: If this service's process is killed while it is started (after returning fromonStartCommand
), Then it will be scheduled for a restart and the last delivered Intent re-delivered to it againonStartCommand
. This Intent will remain scheduled for redelivery until the service CILSstopSelf(int)
With the start ID providedonStartCommand
. The service will not receiveonStartCommand(Intent, int, int)
Call with a null Intent because it will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).