First look at an Android system startup flowchart:
One of the two most important indicators of a process is the initiation of the binder thread pool, which can process binder interprocess communication. The other is to start the handler message loop and use the message loop mechanism.
1. When did the Systemserver process implement the above two mechanisms? See Code:
The binder thread pool is started. is a child thread pool.
public static final void Zygoteinit (string[] argv) throws Zygoteinit.methodandargscaller { ... Zygoteinitnative (); ......}
Started the hander message loop mechanism, which is the handler of the child thread:
public static final void Init2 () { slog.i (TAG, "entered the Android system server!"); Thread thr = new Serverthread (); Thr.setname ("Android.server.ServerThread"); Thr.start (); }
Class Serverthread extends Thread {@Override public void run () {eventlog.writeevent (Eventlogtags.boot_progress_ System_run, Systemclock.uptimemillis ()); Looper.prepare (); ...... try {... SLOG.I (TAG, "Activity Manager"); Context = Activitymanagerservice.main (factorytest);//Initialize Activitytask ... SLOG.I (TAG, "package Manager"); PM = Packagemanagerservice.main (context, factorytest! = Systemserver.factory_test_off);//Initialize Packageman Gerservice activitymanagerservice.setsystemprocess (); ... wm = Windowmanagerservice.main (context, power, factorytest! = Systemserver.factory_tes T_low_level); Servicemanager.addservice (Context.window_service, WM); ...... ((Activitymanagerservice) Activitymanagernative.getdefault ()). Systemready (New Runnable () {Publi C void Run () {}}); Looper.loop (); SLOG.D (TAG, "System Serverthread is exiting!"); }}
2. When does the application process implement the above two mechanisms? See Code:
The binder thread pool is started, and, as above, it is a child thread.
public static final void Zygoteinit (string[] argv) throws Zygoteinit.methodandargscaller { ... Zygoteinitnative (); ......}
started the hander message loop mechanism, which is the handler of the main thread:
public static final void main (string[] args) { samplingprofilerintegration.start (); Process.setargv0 ("<pre-initialized>"); Looper.preparemainlooper (); if (Smainthreadhandler = = null) { Smainthreadhandler = new Handler (); } Activitythread thread = new Activitythread (); Thread.attach (false); .... Looper.loop (); ...... }
3, we look at the start Laucher interface program.
((Activitymanagerservice) Activitymanagernative.getdefault ()) . Systemready (New Runnable () {public void run () { } });
Refer to the Android system source code scenario analysis Book, the final Systemready finally executed to this block:
Boolean starthomeactivitylocked () {if (mfactorytest = = Systemserver.factory_test_low_level &&am P Mtopaction = = null) {//We is running in factory test mode, but unable to find//the factory test App, so just sit around displaying the//error message and don ' t try to start anything. return false; } Intent Intent = new Intent (mtopaction, mtopdata! = null?) Uri.parse (Mtopdata): null); Intent.setcomponent (mtopcomponent); if (mfactorytest! = systemserver.factory_test_low_level) {intent.addcategory (intent.category_home); } activityinfo ainfo = Intent.resolveactivityinfo (Mcontext.getpackagemanager (), STOCK _pm_flags); if (ainfo! = null) {intent.setcomponent (New componentname (aInfo.applicationInfo.packageNam E, ainfo.name)); Don ' t do this if the home app is Currently being//instrumented. Processrecord app = getprocessrecordlocked (Ainfo.processname, AInfo.applicationInfo.uid); if (app = = NULL | | app.instrumentationclass = = NULL) {Intent.setflags (Intent.getflags () | Intent.flag_activity_new_task); Mmainstack.startactivitylocked (NULL, intent, NULL, NULL, 0, ainfo, NULL, NULL, 0, 0, 0, False, Fals e); }} return true; }This is the execution code of the mmainstack.startactivitylocked executed in Systemserver's child thread, please refer to theAndroid System source code scenario analysis a book, the execution differs in resumetopactivitylocked this method, because mresumedactivity equals null, so at this time execution, Startspecificactivitylocked (Next, True, true). The last execution to startprocesslocked, inside this sentence is the key.
int pid = Process.Start ("Android.app.ActivityThread", msimpleprocessmanagement app.processName:null, uid, uid,< C2/>gids, debugflags, NULL);
Message msg = Mhandler.obtainmessage (proc_start_timeout_msg); Msg.obj = app; Mhandler.sendmessagedelayed (msg, proc_start_timeout);
where Mhander is final Handler mhandler = new Handler () {}, this Handler is serverthread in the Handler child thread. When sending a message, only Looper.prepare () is called, and Looper.loop () is called after returning ;
Process.Start creates an application process that opens the binder thread pool and the handler message loop mechanism, refer to 2nd.
1, Thread.attach (false) is launcher the main thread and systemserver child thread binder interprocess communication, send Attach_application_tranction;systemserver child thread ( Binder thread pool) removes the message inside the handler of the Serverthread child thread (Handler message loop) in Systemserver.
2. Systemserver sub-thread sends schedule_launch_activity_tarnsaction to laumcher child thread, then systemserver child thread continues to wait and sends the return data to the launcher child thread.
3, Laucher the main thread returned after the call Looper.loop (), the main thread message loop mechanism was formally established.
4, processing just by Systemserver sub-thread sent Lanncher sub-thread data (one-way), launcher child thread finally through the message processing mechanism, in the main thread called the Launcher OnCreate method.
Android system Boot Process