Complete analysis of Android service startup process

When I first learned about service, I thought it was a thread encapsulation and could also perform time-consuming operations. In fact, the service is running on the main thread. Direct execution of time-consuming operations will block the main thread. It took a long time to directly ANR.

We know that service can perform some background tasks. Background tasks are not time-consuming tasks. There are differences between background tasks and time-consuming tasks. In this way, it is easy to think of music player and weather forecast applications that use service. Of course, if you want to perform time-consuming operations in the service, you can open a thread.

There are two kinds of service running states: startup state and binding state. The two states can be used together. To start a service, just call the startservice method of context and pass in an intent. It seems very simple to say that it is because Android has made a large degree of encapsulation for the convenience of developers. So have you really learned how to start the service? What preparations have been made before the oncreate method callback of the service?

Let's get a general idea from the previous figure. The methods in the same class are framed by the gray background, as shown in the following figure:

Next, analyze the service startup process from the perspective of source code.

Of course, start with the startservice method of context. The implementation class of context is contextimpl. Then we can see the startservice method of contextimpl, as follows:

It will go to the startservicecommon method. Follow up the startservicecommon method.

You can see that activitymanagernative. Is called Start the service with the startservice method of getdefault(), activitymanagernative Getdefault () is activitymanagerservice, AMS for short.

Now the process of starting the service is transferred to activitymanagerservice. We can focus on the startservice method of activitymanagerservice, as follows:

In the above code, the startServiceLocked method of ActiveServices is invoked, then the boot process of Service is transferred from AMS to ActiveServices now.

Continue to follow up activeservices' startservicelocked method as follows:

In the startservicelocked method, the startserviceinnerlocked method will be called again,

Let's look at the startserviceinnerlocked method,

The bringupservicelocked method is called inside the startserviceinnerlocked method. At this time, the startup process is about to leave activeservices. Continue to see the bringupservicelocked method. As follows:

Omitting most if judgments, I believe you must have found the core method, which is realstartservicelocked. Yes, looking at the name is like actually starting a service. Then it's not too late to follow in. As follows:

eureka. app. Thread called schedulecreateservice to start the service, and app Thread is an applicationthread and an internal class of activitythread. At this point, the main thread is reached. Then let's explore the schedulecreateservice method of applicationthread. As follows:

The service component information to be started is wrapped and a message is sent. We focus on this create_ Service message.

This message is received in the handleMessage method, and the handleCreateService method is invoked to follow up the handleCreateService probe.

Finally, this method is very core. A little analysis

First, obtain a loadedapk object, obtain a class loader through the loadedapk object, and create a service through the class loader. As follows:

Then call the createappcontext method of contextimpl to create a contextimpl object.

Then call the makeapplication method of loadedapk to create an application. The creation process is as follows:

Of course, there is only one application, as can be seen from the above code.

After returning to the handlecreateservice method, the service calls the attach method and associates contextimpl, application, etc

Finally, the service called back the oncreate method,

This service is added to a list for management.

At this point, the service is started. The above is the start process of the service.

You may also want to know how the onstartcommand method is called back? You may have noticed that there is also a sendserviceargslocked method in the realstartservicelocked method of activeservices. Yes, that's the entrance.

So let's follow up the sendserviceargslocked method to see how the onstartcommand method callbacks.

You can see that the callback process of onstartcommand method is very similar to that of oncreate method, and will go to app thread。 Now follow up the scheduleserviceargs of applicationthread. You may also guess that it should encapsulate some service information, then send a message and receive it with handlemessage. Yes, the source code is as follows:

Eh, really. The answer should be in the handleserviceargs method, so take a look. The source code is as follows:

You can see that the onstartcommand method is called back.

The above is the source code analysis of the service startup process.

From this, while I understand the service startup process, my ability to read the source code has also improved. When analyzing the source code, I am not able to understand every variable and every method. I focus on some key words. For example, this article is start and service. There will be that feeling. It's right here. Of course, if you fall into an alley, you should also come out.

Such analysis can also find out the overall process. For details, I'll study it when I have a solid foundation.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>