Android Message Processing source code Analysis (2)

Source: Internet
Author: User

Android Message Processing source 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) {//checks if there is a looper, throws an exception if any, and if not, creates a Looper instance to save if (sTh        Readlocal.get ()! = null) {throw new RuntimeException ("Only one Looper could 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 ();        }}//Run Message Queuing in this thread, call quit () 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, it handles if (Mcallback.handlemessa                GE (msg)) {//Call the implementation method of the Mcallback interface return;   }} handlemessage (msg); Finally, call the handler itself overloaded Handlemessage method}} to distribute the message function, the message will first check whether it has to handle its own callback runnable, if any, it is handled by it, if not, it will check whether the handler has its own callback processing, if there is a In the case of a handlemessage method that calls itself overloaded, the build of//handler is always related to the current thread, and if there is no looper in the current thread, an error occurs, and the UI thread defaults to the function public Handler that produces the Looper (    {This (null, false); }//using the specified Looper (which can handle messages in that Looper thread) without the default removal from the current thread looPer public Handler (Looper Looper) {This (Looper, NULL, FALSE); }   ...}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Message Processing source code Analysis (2)

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.