Asynchronous message processing in Android consists of four parts, message, Handler, MessageQueue, Looper. These four sections are briefly described below.
1. Message:
A message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading. It is common to use the What field of the Message to carry commands, in addition to using the ARG1 and
The Arg2 field carries some shaping data and uses the Obj field to carry a single Object object.
2. Handler:
Handler, as the name implies, is the meaning of the processor, which is primarily used to send and process messages. Sending a message is typically using the Handler sendMessage () method, and the emitted message is eventually passed to the Handler Handlermessage () method after a series of tossing and processing.
3. MessageQueue:
MessageQueue is the meaning of Message Queuing, which is primarily used to store all messages sent through Handler. This part of the message will remain in the message queue and wait for it to be processed. There will only be one MessageQueue object in each thread.
4. Looper:
Looper is the steward of MessageQueue in each thread, and after calling the Looper loop () method, it enters an infinite loop, and then whenever a message is found in MessageQueue, it is removed and passed to the Handler Han The Dlemessage () method. There will only be one Looper object in each thread.
------------------------------the asynchronous message processing process--------------------------------------
Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener { Public Static Final intUpdate_text = 1; PrivateTextView text; PrivateButton Changetext; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseUpdate_text://perform UI actions hereText.settext ("Nice-Meet You"); Break; default: Break; } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Text=(TextView) Findviewbyid (R.id.text); Changetext=(Button) Findviewbyid (R.id.change_text); Changetext.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.change_text:NewThread (NewRunnable () {@Override Public voidrun () {//changing UI in a child thread causes the program to crash//text.settext ("Nice to Meet You");Message Message=NewMessage (); Message.what=Update_text; //send the Message object outhandler.sendmessage (message); }}). Start (); } }}
Message Use example
You first need to create a Handler object in the main thread and override the Handlemessage () method.
Then, when UI action is required in a child thread, a message object is created and sent out through Handler.
The message is then added to the MessageQueue queue for processing, while Looper attempts to remove the pending message from the MessageQueue
Finally, distribute back to Handler in the Handlemessage () method.
Since Handler is created in the main thread, the code in the Handlemessage () method is also run in the main thread, so that the UI can be manipulated with peace of mind.
After a message has been called by such a process, it goes from the child thread to the main thread, from the inability to update the UI to the updatable UI, and the core idea of the entire asynchronous message processing.
Analysis of Android asynchronous message processing mechanism