Meaning of onStartCommand () function return value in Android Service, androiddialog Return Value

Source: Internet
Author: User

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 callonStartCommandAfter 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_STICKYThat does not guarantee thatonStartCommandWill 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 itsonStartCommandIs 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).



Related Article

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.