<span id="Label3"></p><p><p><b>Basic Concepts:</b></p></p><p><p>Message: messages that contain the message id, the message processing object, and the processed data are queued by MessageQueue and processed by Handler.</p></p><p><p>Handler: handler, responsible for sending and processing the Message. When using handler, you need to implement the Handlemessage (message Msg) method to process a specific message, such as updating the Ui.</p></p><p><p>MessageQueue: message queuing, which holds messages sent by handler and executes according to FIFO Rules. of course, storing a message is not a meaningful preservation, but rather a concatenation of the message in the form of a linked list, waiting for the looper to be Extracted.</p></p><p><p>Looper: the message pump continuously extracts message execution from the Messagequeue. therefore, a MessageQueue needs a looper.</p></p><p><p>Thread: threads that are responsible for dispatching the entire message loop, that is, the execution site of the message loop.</p></p><p><p><b>Three-step Implementation of the Thread's message handling mechanism:</b></p></p><p><p>1. Create looper with Looper.prepare () (message pump, which continuously extracts message execution from MessageQueue (with a message pool size of 10))</p></p><p><p>2. Processing messages with Handler</p></p><p><p>3. Enter the loop with Looper.loop ()</p></p><p><p>* This mechanism is provided by default on the main thread, and other threads do not have looper objects by Default. If you want our newly created thread to have a looper object, we should first call the Looper.prepare () method in the inline run () method, and then call the Looper.loop () method at the end of the run () method. Typical usage is as follows:</p></p><p><p>Class Looperthread extends Thread {</p></p><p><p>Public Handler mhandler;</p></p><p><p>public void Run () {</p></p><p><p>Looper.prepare ();</p></p><p><p>Mhandler = new Handler () {</p></p><p><p>public void Handlemessage (Message Msg) {</p></p><p><p>Process incoming messages here</p></p><p><p>}</p></p><p><p>};</p></p><p><p>Looper.loop ();</p></p><p><p>}</p></p><p><p>}</p></p><p><p>* If the Looper object already exists in our thread, we can get it through looper.mylooper (), and we can also get the Looper object of the main thread in the current application via Looper.getmainlooper ().</p></p><p><p><b>handlerthread:</b></p></p><p><p>Handlerthread is a packaged thread class with Looper that inherits the thread class, which is essentially a threading that you can use in a non-ui thread to implement a message mechanism, as Follows:</p></p><p><p>Handlerthread thread = new Handlerthread ("myhandlerthread"); Create and start a thread</p></p><p><p>Thread.Start ();</p></p><p><p>Mhandler = new Handler (thread.getlooper ()); Looper objects in Handlerthread are passed in when the handler object is created</p></p><p><p>Mhandler.post (new Runnable () {...});</p></p><p><p>precautions <b>:</b></p></p><p><p>* When thread a needs to send a message to thread b, thread B instantiates the handler class with its own looper, and we need to ensure that the thread contains looper when instantiating the handler in the Process.</p></p><p><p>* The message class should be instantiated in thread a, and then sent to thread B processing using the method of the handler class SendMessage (to instantiate the handler class First)</p></p><p><p>* Two methods of instantiating the message class: new message ()/calling the Obtainmessage () method in the handler class (to instantiate the handler class First)</p></p><p><p>Message msg = Handler.obtainmessage ();</p></p><p><p>MSG.ARG1 = i;</p></p><p><p>Msg.sendtotarget ();</p></p><p><p>Message Msg=new message ();</p></p><p><p>msg.arg1=i;</p></p><p><p>Handler.sendmessage (msg);</p></p><p><p>* The first notation is that message is obtained from the handler class, so that messages can be sent directly to the handler object, and the second is to call Handler's SendMessage method to send the message directly.</p></p><p><p>* The Handler.post (runnable) method and the Handler.sendmessage () method have the same principle, except that the runnable is turned into a message. When the current thread is not determined (which Looper is not known), the View.post (Runnable Action) method is called as much as possible when updating the Ui. In this method, view automatically obtains the handler of the current thread (that is, the UI thread) and then posts the action object into the Handler. In handler, it wraps the action object passed in as a message (the callback of the message is the Action) and then puts it into the message loop of the UI Thread. When handler processes the message again, there is a branch (the one that is not explained) that is set for it to call the Runnable Run method directly. At this point, it has been routed to the UI thread, so we can update the UI with no Worries.</p></p><p><p>Android Message Processing</p></p></span>
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