Onstartcommand () function return value meaning in Android Service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.