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