Details of the Onstartcommand () method inside the service

Source: Internet
Author: User

When the service is started, the OnCreate method is called only the first time, Onstartcommand and OnStart are called each time. Onstartcommand will tell the system how to restart the service, such as determining whether to restart after an abnormally terminated, and under what circumstances the abnormal termination

Onstartcommand and OnStart differences

This was the old OnStart method that would be called on the pre-2.0
Platform. On 2.0 or later we override Onstartcommand () so this
Method won't be called.
After 2.0 API level, implementing onstart is equivalent to overriding Onstartcommand and returning Start_sticky
@Override
public void OnStart (Intent Intent, int startid) {
Handlecommand (Intent);
}

After 2.0 API level, the OnStart () method is replaced by Onstartcommand ()
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Handlecommand (Intent);
We want this service to continue running until it is explicitly
Stopped, so return sticky.
return start_sticky;
}

Oncreate,onstartcommand,onstart is executed sequentially when the service is started, and if the service is restarted before the system shows calls to StopService and Stopself, the Onstartcommand is called. Onstartcommand,onstart is executed sequentially when the service is restarted. At any time, Onstartcommand () is called First, and OnStart () is called.

Onstartcommand return value

When Onstartcomand is used, it returns an (int) shaping.
This shaping can have four return values: Start_sticky, Start_no_sticky, Start_redeliver_intent, start_sticky_compatibility.
Their meanings are:
1): Start_sticky: If the service process is killed, the status of the reserved service is the start state, but the delivery intent object is not preserved. The system then tries to recreate the service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null.
2): Start_not_sticky: "Non-sticky". With this return value, if the service is aborted after executing Onstartcommand, the system does not automatically restart the service
3): start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed by an exception after Onstartcommand is executed, the service is automatically restarted and the value of intent is passed in.
4): Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be restarted after kill.

Onstartcomand parameter flags meaning

Flags indicates how the service is started:
Additional data about this start request. Currently either 0, Start_flag_redelivery, or start_flag_retry.

Start_flag_redelivery: If you implement Onstartcommand () to schedule asynchronous work or work in another thread, you may need to use Start_flag_redelivery to have the system resend a intent. This way, if your service is killed when it is processed, intent will not be lost.
Start_flag_retry: Indicates that the service was set to Start_sticky before this token is passed in.
When the service is started, the OnCreate method is called only the first time, Onstartcommand and OnStart are called each time. Onstartcommand will tell the system how to restart the service, such as determining whether to restart after an abnormally terminated, and under what circumstances the abnormal termination

Onstartcommand and OnStart differences

This was the old OnStart method that would be called on the pre-2.0
Platform. On 2.0 or later we override Onstartcommand () so this
Method won't be called.
After 2.0 API level, implementing onstart is equivalent to overriding Onstartcommand and returning Start_sticky
@Override
public void OnStart (Intent Intent, int startid) {
Handlecommand (Intent);
}

After 2.0 API level, the OnStart () method is replaced by Onstartcommand ()
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Handlecommand (Intent);
We want this service to continue running until it is explicitly
Stopped, so return sticky.
return start_sticky;
}

Oncreate,onstartcommand,onstart is executed sequentially when the service is started, and if the service is restarted before the system shows calls to StopService and Stopself, the Onstartcommand is called. Onstartcommand,onstart is executed sequentially when the service is restarted. At any time, Onstartcommand () is called First, and OnStart () is called.

Onstartcommand return value

When Onstartcomand is used, it returns an (int) shaping.
This shaping can have four return values: Start_sticky, Start_no_sticky, Start_redeliver_intent, start_sticky_compatibility.
Their meanings are:
1): Start_sticky: If the service process is killed, the status of the reserved service is the start state, but the delivery intent object is not preserved. The system then tries to recreate the service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null.
2): Start_not_sticky: "Non-sticky". With this return value, if the service is aborted after executing Onstartcommand, the system does not automatically restart the service
3): start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed by an exception after Onstartcommand is executed, the service is automatically restarted and the value of intent is passed in.
4): Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be restarted after kill.

Onstartcomand parameter flags meaning

Flags indicates how the service is started:
Additional data about this start request. Currently either 0, Start_flag_redelivery, or start_flag_retry.

Start_flag_redelivery: If you implement Onstartcommand () to schedule asynchronous work or work in another thread, you may need to use Start_flag_redelivery to have the system resend a intent. This way, if your service is killed when it is processed, intent will not be lost.
Start_flag_retry: Indicates that the service was set to Start_sticky before this token is passed in.

Details of the Onstartcommand () method inside the 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.