A few key concepts
1, MessageQueue: is a data structure, see the name of righteousness, is a message queue, the place to store messages. Each thread can have a maximum of one MessageQueue data structure.
When a thread is created, its MessageQueue is not created automatically. A Looper object is typically used to manage the MessageQueue of the thread. When the main thread is created, a default Looper object is created, and a message Queue is created automatically when the Looper object is created. Other non-main threads do not automatically create Looper, and are implemented by calling the prepare function when needed.
2. Message: The object that is stored in the messages queue. A message queue contains multiple message.
The acquisition of the message instance object, usually using the static method obtain () in the message class, has multiple overloaded versions to choose from, and it is not necessarily created to create a new instance directly, but rather first from the message pool See if there is an instance of message available, and then go directly out and return to this instance. If there is no message instance available in the message pool, a message object is created with the given parameter. When Removemessages () is called, the message is removed from the message queue and placed in the message pool. In addition to this, you can get a message instance from the handler object's Obtainmessage ().
3, Looper:
Is the manager of MessageQueue. Each MessageQueue can not be separated from the Looper, Looper object is created by the prepare function to achieve. At the same time, each Looper object
associated with a thread. The Looper object of the current thread can be obtained by calling Looper.mylooper ()
When you create a Looper object, a MessageQueue object is created at the same time. In addition to the default looper of the main thread, other threads do not have MessageQueue objects by default, so the message cannot be accepted. If you need to accept, you define a Looper object (via the Prepare function) so that the thread has its own Looper object and MessageQueue data structure.
Looper remove the message from the MessageQueue and leave it to Handler's handlemessage for processing. When processing is complete, call Message.recycle () to put it in the message pool.
4, Handler:
The processor of the message, handler, is responsible for encapsulating the information that needs to be passed into a message, by invoking the Obtainmessage () of the handler object;
The message is passed to Looper, which is implemented by the handler object's SendMessage (). The message is then placed into the MessageQueue by Looper.
When the Looper object sees that the MessageQueue contains a message, it broadcasts it. After the handler object receives the message, it calls the corresponding handler object's Handlemessage () method to process it.
The handler is associated with a separate thread and message queue. Handler The default association main thread, although to provide the runnable parameter, the default is to call the run () method in runnable directly. That is, by default it will be executed in the main thread, and the interface will be stuck if the operation in this area is blocked. If you want to execute on another thread, you can use the Handlerthread
Own summary looper is equivalent to MessageQueue's creator and administrator. While handler is the handler, it handles transactions at both ends of the MessageQueue.
Each thread has a looper,
Looper has a message queue
Looper is a dead loop that constantly processes messages in the message queue.
Handler is a data structure created to facilitate multi-threading or asynchronous processing.
It has two members, a looper that points to the thread that handler was created, and one that is the message queue for that thread.
So, calling handler's post method is actually putting the message on the thread's message queue (note that not the current thread, if you create the handler in the main thread, call handler's Post method in the child threads, The message is actually sent to the main thread of the message queue) (the "post to handler to be created, the place where the handler belongs to"); Each message has a target member, and handler sends a past message that calls the target setting of the message itself.
Looper receives the message and sends the message to target, which means the message turns round and back to Handler,handler. This process seems to be a detour, in fact it is necessary to call handler post is in the sub-thread, and handler processing method is called in the main thread, this process is accompanied by a thread scheduling.
Handlerthread is inherited thread, the main role is to establish a thread, and the creation of a message queue, with its own looper, lets us distribute and process messages in our own thread.
Android message processing through handler and Looper, Handlerthread not only can provide asynchronous processing, handler processing the message method will also be executed in this thread, his most important role is to provide a thread. Http://www.eoeandroid.com/thread-193224-1-1.html
To use Looper to build the Android messaging mechanism, thread is actually able to implement the Handlerthread function by code, But you see Handlerthread's API Description: Handy class for starting a new thread, which has a looper. The looper can then is used to create handler classes. Note that start () must still is called. It can be said that it is a class that simplifies the thread class code that needs to implement the message mechanism, and helps you write down the necessary code. In addition, the Onlooperprepared method can help you do some looper start the loop before the initialization work, the code is clearer. http://zhidao.baidu.com/question/334647488.html