Illustration of the two methods Service

Illustration of the two methods Service

As we all know, Service, there are two ways to start
Here Insert Picture Description

.
.
.

A. Start Service in a new process

In the new process starts Service is divided into five steps:

step1:

App sends a message to the AMS Service start from AIDL perspective looks like this:
service
Figure client represents App end, sent by AMN AMP APP to be started / service information to the AMS.

Step2:

AMS inspection Service is declared in the Manifest, the statement did not directly error.

Start Service AMS inspection process exists, if not, put down the Service information is stored, and then create a new process.

In AMS, each Service, use ServiceRecord object to hold.

Step3:

After the new process starts, will create a new ActivityThread, then ActivityThread objects by passing AMP to AMS, told the AMS, the new process started successfully.

Step4:

The AMS-entered the ActivityThread objects transformed into ApplicationThreadProxy, that is, ATP, by ATP, to send information to the Service you want to start a new process.

Step5:

AMS new process information received through ApplicationThread, and the final step of the previously described start Activity same, by means of ActivityThread and H, the implementation of the method onCreate Service. In the meantime, created a Service Context context object, and is associated with the Service.
Here Insert Picture Description
Here H and H Activity during start is with a Handler class ... here to start the service successfully in the new process
.
.
.

II. Start Service in the current process

Compared to the above process, start the Service in the current process is much simpler:
1.App request Start Service to AMS

2.AMS routine checks, such as whether to declare the Service, the Service registered here at AMS. AMS is found to start Service App where the Service, on notice to start this App Service.

3 ... Service started successfully
.
.

III. Binding newly launched Service in the current process

Step1.APP binding request to the AMS Service

Step2.AMS routine checks, such as whether to declare the Service, the Service registered here at AMS. AMS is found to start Service App where the Service, on notice to Service App bind.

Step3.App AMS received the first message, start the Service

Step4.App AMS received a second message, binding service, and a binder passed AMS

After Step5.AMS received binder, send it to the APP

Step6.APP received binder objects, Service starts successfully

Step1:
Here Insert Picture Description
step5,6:
Here Insert Picture Description
The picture is painted himself, the address is: processon

Reference article:
https://www.jianshu.com/p/058d35e01dd1

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104691794