Android-Asynchronous Message Processing Mechanism preliminary, android-asynchronous mechanism
Android asynchronous Message processing consists of four parts: Message, Handler, MessageQueue, and logoff.
Message:A message transmitted between threads. It can carry a small amount of information internally and is used to exchange data between different threads. The arg1 and arg2 fields can be used to carry some integer data. shiyongobj carries an Object.
Handler: The processor, mainly used to send and process messages. The sendMessage () method is used to send information. After a series of processing operations, it is passed to the handleMessage () method.
MessageQueue: Message Queue, mainly used to store all messages sent by Handler. This part of messages will always exist in the message queue and will be processed. Each thread has only one MessageQueue object.
Logoff: The MessageQueue manager in each thread. after calling the logoff loop () method, it will enter an infinite loop, as longMessageQueueIf a message exists, it is taken out and passed to the Handler's hanleMessage () method. Each thread also has only one logoff object.
The process of Asynchronous Message Processing Mechanism (taking changing the UI as an example ):
Create a Handler object in the main thread and rewrite the handlerMessage () method as needed. When the sub-thread performs UI operations, a Message object is created, which is sent out through the Handler object and added to MessageQueue for processing, logoff will always try to retrieve this Message from MessageQueue, and finally distribute it back to Handler's handleMessage () method.