Flexible application of message in Android (reprint)

Source: Internet
Author: User
Tags prepare static class
Quote from Easyandroid Forum, original: http://www.easyandroid.com/bbs/viewthread.php?tid=33
1. The message mechanism to utilize the communication between Android threads

1.1.Message
The code is in the Frameworks\base\core\java\android\os\message.java.

Message.obtain function: There are several obtain functions, the main function is the same, but the parameters are different. The effect is to take a message out of the pool and, if there are no more words in the pool, create a new one, and assign a value to the resulting message object with the corresponding argument.

Message Pool: size 10; via message.mpool-> (Message and Message.next)-> (Message and Message.next)-> (Message and Message.next) ... Constructs a message Pool. The first element of the message pool is directly new, and then the Message.mpool (the static variable of the static Class) is pointed to it. The rest of the elements are finished using the message's recycle function to clean it up and put it in the message pool (implemented via the message pool's last message, next to the message that needs to be recycled). The following figure is the structure of the message pool:


1.2.MessageQueue
MessageQueue has a column for the message that is received:

Messagequeue.mmessages (static variable)-> (message and Message.next)-> (Messages and Message.next), ..., and the following figure is to receive message queues:

The upper-layer code will eventually invoke the MessageQueue enqueuemessage function by putting a message into the MessageQueue via a handler SendMessage function. Enqueuemessage the received message into the queue based on the construction of the queue of the message received above.

The MessageQueue removemessages function deletes the received message from the queue based on the construction of the queue of the message received above. and call the Recycle function of the corresponding message object to put the unused message into the message pool.

1.3.Looper
The Looper object is created through the prepare function, and each Looper object is associated with a thread

Java code public static final void prepare () {if (sthreadlocal.get ()!= null) {throw new runtimeexception       ("Only one looper could created per thread");   } sthreadlocal.set (New Looper ()); }

The Looper object creates a MessageQueue, and the main thread defaults to create a looper that has MessageQueue, and other threads do not have MessageQueue to receive messages by default. If you need to receive a message, you need to create a MessageQueue through the prepare function. See the sample code for a specific action.

Java Code Private Looper () {mqueue = new MessageQueue ();       Mrun = true;   Mthread = Thread.CurrentThread (); }

The Preparemainlooper function only calls the main thread (System processing, programmers do not have to deal with it), it invokes prepare to establish Looper objects and MessageQueue.

Java code public static final void Preparemainlooper () {prepare ();       Setmainlooper (Mylooper ());       if (process.supportsprocesses ()) {Mylooper (). mqueue.mquitallowed = false; }   }

The loop function takes a message out of the MessageQueue and then processes it through the handler DispatchMessage function (the processing of the visible message is handler responsible), After the message has been processed, it is then put into the messages pool through the Recycle function of the messaging object, so that the next time it is used, a certain amount of memory management is provided through pool processing to expedite the acquisition of the message object. See the next function of MessageQueue for how to do timed processing of messages that need to be processed regularly. It takes message to do processing, by judging whether the MessageQueue inside of the messages meet the time requirements to decide whether to need to take out to do processing, this way to do the timing of information processing.

Java code    public static final void loop ()  {        looper me = mylooper ();       messagequeue queue =  me.mQueue;       while  (true)  {            message msg = queue.next (); // might block            //if  (!me.mrun)  {            //    break;            //}           if  (msg !=  NULL)  {               if  ( Msg.target == null)  {                     // no target is a magic identifier for the quit  message                    return;               }                   if  (me.mlogging != null)                      me.mlogging.println (">>>>> Dispatching to "  +  msg.target +  " " + msg.callback +  ": "  + msg.what);               msg.target.dispatchmessage (msg);                if  (me.mlogging!= null )                      Me.mLogging.println ("<<<<< finished to"  + msg.target +  " " +  msg.callback);                Msg.recycle ();           }        }  }  

1.4.Handler

The handler constructor indicates that handler will have member variables pointing to Looper and MessageQueue, and we will see that there is little need for these references, and that the callback is the object that implements the callback interface, and then see the role of the object.

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.