Onstartcommand () is called by the Android system and is essentially called the OnStart () method.
There are several Onstartcommand () return values:
1)Start_sticky
English Explanation:
Constant to return from onStartCommand
: If this service's process is killed while it's started (after returning from onStartCommand
) and then L Eave it in the started state but don ' t retain this delivered intent. Later the system would try to re-create the service. Because It is in the started state, it'll guarantee to call after onStartCommand
creating the new service instance; if there n OT any pending start commands to being delivered to the service, it'll be called with a null intent object, so you must tak E Care to check for this.
This mode makes sense for things that would be explicitly started and stopped to run for arbitrary periods of time, such as A service performing background music playback.
Note: When the return value is Start_sticky, the service will only stop running when we want it to end. The system will re-create a new service even if the service's process is killed.
2) start_sticky_compatibility
Compatibility version of that does not guarantee that would be START_STICKY
onStartCommand
called again after being killed.
Description: Start_sticky compatible version, but will not be guaranteed to restart after kill
3) Start_not_sticky
Constant to return from Onstartcommand
: If this service's process is killed while it's started (after RET Urning from Onstartcommand
), and there is 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 call to Context.startservice (Intent)
. The service would not receive a Onstartcommand (Intent, int., int)
call with a null Intent because it won't be re-started if there is no pending Intents to deliver.
This mode makes sense for things that want to does some work as a result of being started, but can is stopped when under mem Ory pressure and would explicit start themselves again later to doing more work. An example of such a service would is one that polls for data from a server:it could schedule an alarm to poll every N mi Nutes by has the alarm start its service. When onStartCommand
the it was called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to does its networ King. If its process is killed while doing that check, the service would not be restarted until the alarm goes off.
Note: Contrary to Start_sticky, the new service will not be restarted after being kil off. It is useful for tasks that can run in the case of a system resource (such as memory) and can stop if the resource is not well-enough (for example, if it is not sufficient).
4) Start_redeliver_intent
Constant to return from onStartCommand
: If this service's process is killed while it's started (after returning from onStartCommand
), then I t be is scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand
. This Intent would remain scheduled for redelivery until the service calls with the stopSelf(int)
start ID provided to onStartCommand
. The service won't receive a call with onStartCommand(Intent, int, int)
a null Intent because it would be re-started if it's not finished Processing all Intents sent to it (and any such pending events'll be delivered at the point of restart).
Onstartcommand () function return value meaning in Android Service