Asynchronous message processing in Android consists of four parts, message, Handler, MessageQueue, and Looper.
1.Message.
Messages that are passed between threads can carry a small amount of information inside to exchange data between different threading. You can use the What field, Arg1 and arg2 carry some integer data, and the obj field carries a single Object object.
2.Handler.
Handler, which the processor means, is primarily used to send and process messages. Sending a message typically uses 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.
Message Queuing, which is used primarily to store all messages sent through handler, is kept in the message queue, waiting to be processed, with only one MessageQueue object in each thread.
4.Looper.
Looper is the steward of messsagequeue in each thread, and after calling the Looper loop () method, it goes into a wireless loop, and then whenever a message is found in MessageQueue, it is removed. and passed to the handler Handlermessage () method. Only one Looper object will exist in each thread.
The process for processing asynchronous messages using handler is as follows:
1. Create a handler object in the main thread and override the Handlermessage () method.
2. When a UI update operation is required in a child thread, a message object is created and sent through the SendMessage () method of the Handler object
3. Messages sent out are added to the queue of MessageQueue waiting to be processed.
4.Looper will always try to remove messages waiting to be processed from messagequeure and distribute them to Handler's Handlermessage () method.
5. Because handler is created in the main thread, the code in the Handlermessage () method is also run in the main thread to update the UI action.
Asynchronous message processing mechanism of Android development handler