Android Message Processing source code Analysis (1) Click the Open link
Continue to analyze the remaining class files
Looper.javapublic Final class Looper {final MessageQueue mqueue; Message Queue final Thread mthread; Looper contact thread public static void prepare () {prepare (true); The private static void Prepare (Boolean quitallowed) {//will check for Looper first. Throws an exception if it does. If not, create a Looper instance to save if (Sthreadlocal.get ()! = null) {throw new RuntimeException ("Only one Looper may Be created per thread "); } sthreadlocal.set (New Looper (quitallowed)); public static void Preparemainlooper () {prepare (false); Synchronized (Looper.class) {if (smainlooper! = null) {throw new IllegalStateException ("The MA In Looper have already been prepared. "); Smainlooper = Mylooper (); }}//Execute Message Queuing in this thread. Call quit () to stop public static void loop () {... final MessageQueue queue = Me.mqueue; Make sure the identity of the the the the The local process,//and keep track of what the IdentITY token actually is. Binder.clearcallingidentity (); Final Long ident = Binder.clearcallingidentity (); for (;;) {Message msg = Queue.next ();//Remove a message from message queue if (msg = = NULL) {//No message indica TES that, the message queue is quitting. Return }//This must is in a local variable, in case a UI event sets the logger Printer logging = Me.mlogg ing if (logging! = null) {logging.println (">>>>> dispatching to" + Msg.target + "" + Msg.callback + ":" + msg.what); } msg.target.dispatchMessage (msg); Handler distributed message processing to MSG ...} Taking out the current thread's Looper, returning NULL indicates that the current thread does not have Looper public static Looper Mylooper () {return sthreadlocal.get (); }}
Handler.javapublic class Handler {//Definition Callback Interface public interface Callback {public boolean handlemessage (Me Ssage msg); }//Subclass the message processing method to be implemented public void Handlemessage (Message msg) {} * Handle system messages here. */public void DispatchMessage (message msg) {//Distribution information if (Msg.callback! = null) {//if specified MSG.CALLB ACK, then it processes Handlecallback (msg); } else {if (mcallback! = null) {//if Handler.mcallback is specified. Then it handles the IF (Mcallback.handlemessage (msg)) {//Call Mcallback interface implementation method return; }} handlemessage (msg); Finally, the handler self-overloaded Handlemessage method} is called to distribute the message function, and the message first checks that it has no callback runnable to handle itself, and if so, it handles it. If not, it will check if the handler has its own callback processing, if there is a call, if not, call itself overloaded Handlemessage method//handler generation is always related to its current thread, assuming that the current thread does not have a looper. The error will be. The default function in the UI thread that produces looper is public Handler () {This (null, false); }//using the specified Looper (capable of handling messages in that Looper thread). Do not remove the loo from the current thread by defaultPer public Handler (Looper Looper) {This (Looper, NULL, FALSE); } ...}
Android Message Processing source code Analysis (2)