Liu Hao
Blog: Http://blog.csdn.net/liuhaoyutz
Android version: 4.4.2
In this paper, we analyze how the ANDROIDUI thread, the main course, implements the processing of the message.
The implementation class for the UI thread is defined in the Frameworks/base/core/java/android/app/activitythread.java file. Let's look at Android's description of the Activitythread class:
130/**
131 * This manages the execution of the mainthread in an
Application process, scheduling andexecuting activities,
133 * broadcasts, and other operations on itas the activity
134 * Manager requests.
135 *
136 * {@hide}
137 */
The following is the main function of the Activitythread class:
5024 public static void Main (string[] args) {5025 samplingprofilerintegration.start (); 50265027//Closegu ARD defaults to True and canbe quite spammy. We5028//Disable it here, but selectivelyenable it later (via5029//Strictmode) on debug builds, butusing DropBox, not logs.5030 closeguard.setenabled (false); 50315032 Environment.initforcurrentuser (); 50335034 Set the reporter for event loggingin libcore5035 Eventlogger.setreporter (Neweventloggingreporter ()); 50365037 Security.addprovider (Newandroidkeystoreprovider ()); 50385039 process.setargv0 ("<pre-initialized>"); 504050 Looper.preparemainlooper (); 50425043 activitythread thread = Newactivitythread (); 5044 Thread.attach (false); 50455046 if (Smainthreadhandler = = null) {5047 smainthreadhandler =thread.gethandler (); 5048 }50495050 asynctask.init (); 50515052 if (false) {5053 looper.mylooper (). SETmessagelogging (new5054 logprinter (Log.debug, "Activitythread"); 5055}5056 if (! " User ". Equals (Android.os.Build.TYPE)) {5057 Slocallog = new Locallog (+); 5058 looper.mylooper (). Setme Ssagelogging (Slocallog); 5059}50605061 Looper.loop (); 50625063 throw new RuntimeException ("Mainthread Loop unexpectedly exited "); 5064}
5041 lines, call the Looper.preparemainlooper function, which is different from the normal thread call Looper.prepare () function described in the previous article, we look at the implementation of the Looper.preparemainlooper function:
/** *initialize The current thread as a looper, marking it as a *application ' s main looper. The main looper for your application -*is created by the Android environment, so-you-should-never need-*to CA ll this function yourself. Seealso: {@link #prepare ()} * * * 94 public static void Preparemainlooper () { prepare (false); Synchronized (looper.class) {$ if (smainlooper! = null) {98 throw newillegalstateexception ("The main Looper H As already been prepared. "); }100 smainlooper = Mylooper (); 101 }102 }
As you can see, 95 rows, Preparemainlooper first calls prepare (false), so similar to normal threads, except that the passed parameter is false, indicating that the looper cannot exit because the UI thread is not allowed to exit the message loop.
100 lines, call the Mylooper function, get the current looper is the UI thread Looper, saved in the Smainlooper variable, the purpose is, if other threads want to get UI thread looper, only need to invoke the Getmainlooper function.
5046-5048 rows, if Smainthreadhandler is null, call Thread.gethandler (), which returns an H class object Mh,h class Activitythread class, inherits from the handler class, This completes the default handling of many message.
5061 lines, call the Looper.loop () function, enter the message loop processing.
In contrast to the previous article "Android architecture analysis of android messaging mechanism (ii)", analyzed here, We can see that the UI thread is roughly the same as the process of executing message processing on a normal thread, except that it calls Looper.preparemainlooper instead of Looper.prepare, and handler uses the H class object MH.