Android two ways to start service (remote): Bind and start

Source: Internet
Author: User

Introduction: This paper mainly discusses the start of remote service.

Service and activity are not in a project, or in an app. is not in a process, so it will use the aidl.

The Android:process property of the service is not specified.

First, StartService

1. The process of starting the service by calling StartService:

oncreate-"onstartcommand-" OnStart

StartService is used only to start the service, and if the activity needs to communicate with the service, use broadcast.

2, and then, multiple calls to StartService, the service will be executed several times:

Onstartcommand-"OnStart

3, after multiple calls to StartService, call once StopService can end the service. (If you call stopservice more than once, only useful for the first time)

4, call the StopService service end process:

-"OnDestroy

Other than that

Activity start-up process: oncreate-"onstart-" Onresume

Activity exit Process: onpause-"onstop-" OnDestroy

Second, Bindservice

1. The process of starting the service by calling Bindservice:

oncreate-"onbind-" (onserviceconnected)

The bindservice can be used to start a service and enable activity to communicate with the service.

2, multiple calls to Bindservice, the service itself did not take any action.

3, so once Unbindservice will be able to end the service. (If you call unbindservice more than once, it will be useful for the first time, error later)

4, call the Unbindservice service end process:

Onunbind-"OnDestroy

Third, first StartService, after Bindservice

1, call StartService First, then call Bindservice. The process of service execution is:

oncreate-"onstartcommand-" onstart-"onbind-" (onserviceconnected)

2, first Unbindservice, after StopService. The execution of the end of the service process:

Onunbind-"OnDestroy

It should be noted that Unbindservice will execute to Onunbind,stopservice to OnDestroy.

3, first StopService, after Unbindservice. The execution of the end of the service process:

Onunbind-"OnDestroy

Note that StopService will not perform any action, Unbindservice will execute to onunbind-OnDestroy.

Four, first Bindservice, after StartService

1, call StartService First, then call Bindservice. The process of service execution is:

oncreate-"onbind-" (onserviceconnected)-"onstartcommand-" OnStart

2, first Unbindservice, after StopService.

The process of service execution is the same as three.

3, first StopService, after Unbindservice. The execution of the end of the service process:

The process of service execution is the same as three.

V. Summary

1, many times Bindservice, the service itself Onbind will not be executed many times.

2, bind the previous service, execute once Unbindservice is enough. Otherwise it will go wrong.

Android two ways to start service (remote): Bind and start

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.