Android Startup principle Anatomy "Go"

Source: Internet
Author: User

We know that Android is in an activity, but we don't see how an activity starts. Let's start with the source code for Android today.

Activitythread:

When an Android apk is open, the class is the first class to use. Let's start with this class. By the end of this class, you'll be able to understand how Android apps start. The name of the goods is named with a thread ending, which seems to be a threading class. In fact, he is not a threading class, and this class does not inherit any classes. Or, it is directly inherited in the object class. Let's see Activitythread.

 Public Final class activitythread {    ...}

This is the Activitythread definition when the source code, you can see that he did not inherit any class. Then why would he end up with a thread? It seems unreasonable. actually otherwise Presumably everyone should be Android's asynchronous threading mechanism. In fact Activitythread started a thread at the time of creation, which is an asynchronous thread that creates a MessageQueue and polls the message at the same time, so when the application is running, the thread is always there. This thread is the thread of the application, the processing of the UI, etc.
There is a main method for this class in Acitivitythread. We know that the portal to the Java application is the main method. This is the entrance to the program. Let's look at the source code for Activitythread:

 Public Static voidMain (string[] args) {Samplingprofilerintegration.start (); //Closeguard defaults to True and can is quite spammy. We//disable it, but selectively enable it later (via//strictmode) on the debug builds, but the using DropBox, not logs.Closeguard.setenabled (false);        Environment.initforcurrentuser (); //Set The reporter for event logging in LibcoreEventlogger.setreporter (NewEventloggingreporter ()); Security.addprovider (NewAndroidkeystoreprovider ()); Process.setargv0 ("<pre-initialized>"); //the key part, see hereLooper.preparemainlooper (); Activitythread Thread=NewActivitythread (); Thread.attach (false); if(Smainthreadhandler = =NULL) {Smainthreadhandler=Thread.gethandler ();        } asynctask.init (); if(false) {looper.mylooper (). setmessagelogging (NewLogprinter (Log.debug,"Activitythread"));        } looper.loop (); Throw NewRuntimeException ("Main thread loop unexpectedly exited"); }

The rest of us, first of all, look at the key part, which is where I commented. Here we first use the Looper.prepareMainLooper(); method to create a MessageQueue instance. Preparemainlooper internally calls the prepare () method to create a message queue (MessageQueue). Once created, a Activitythread object is created. Inside this Activitythread object, a handler and a Applicationthread object are created, and these two objects are described later, (there should be an interrupt vector, Or then start a thread to explain handler object and Application object, haha) first pressure. After the Activitythread object is created. The middle part is slightly over the table. This will then call Looper.loop (), which is the loop in the message team. So this activitythread is always in, because need to wait for the other line Cheng come over the message. This thread is our main path. The management of Activity,service and so on is here. will be introduced later.
Well, this process must be known to all, now we talk about our handler and Applicationthread. In Activitythread this handler is actually an inner class, the class name is H, yes, is H a letter, he inherits from the handler object. Let's take a look at the source code:

In line 3rd, here we can see that he created an instance of the H class. Note that this is the member variable area of the class, not in a method. This means that the MH object is created at the same time that the class was created. So this MH is equivalent to the main thread, which is created in main. This object is used for other threads that want to send messages to the main thread. Let's take a look at the part of the main function that we didn't mention:

 looper.preparemainlooper (); Activitythread thread  = new          Activitythread ();        Thread.attach ( false  );  if  (Smainthreadhandler = = null  Span style= "color: #000000;"        ) {Smainthreadhandler  = Thread.gethandler (); } asyncta  

We can see in the IF statement that such a statement sMainThreadHandler = thread.getHandler(); thread is the Activitythread instance that was just created, and the GetHandler method returns the MH instance.
Now let's talk about the Applicationthread class, which is the inner class of activitythread, what is the function of this class? In fact, this involves inter-process communication, which is responsible for receiving remote AMS (Activitymanagerservice) IPC (interprocess communication) calls. And this call is usually the Android system to call, this is equivalent to a process of communication, the Android system will have a service to monitor the user's operation, etc., when the user is related to the process through the communication to notify the application to do the corresponding response, such as starting an activity. When this class receives a notification, the activity is then scheduled to start, and we can look at the source code for this class:

Some of the code is posted here, noting that it is not the entire code of this class, but just a part of it. Let's take a look at the second method.
schedulePauseActivityThe method should mean "schedule activity to suspend", which is to have the activity invoke OnPause code. The rest of the code below is similar. Is the code that manages the service and activity, and the inside of these methods calls SendMessage, and the interior of the method continues to call Mh.sendmessage (). Schedules messages that pause or initiate activity to the MessageQueue queue of the main thread, waiting for the message queue to poll to process the message. We can see from the name of these methods, schedule is the meaning of the arrangement, "scheduled suspension of activity" means that you have "suspend activity" this matter to arrange, and so on the MessageQueue at hand, and then to deal with the "suspended activity". And the person who arranges this matter, is MH this assistant, by he sends the message to MessageQueue.

Original address: http://www.2cto.com/kf/201506/404757.html

Android Startup principle Anatomy "Go"

Related Article

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.