Android message mechanism

Source: Internet
Author: User

Android message mechanism

A lot of previous knowledge is always forgotten. It seems that I have not fully understood it, and I hope to record it in the form of a blog.

Speaking of the android message mechanism, we have to mentionHandlerIts Chinese meaning is the controller and processor.
The Handler documentation on Android is written as follows:
Handler can send and process Message and Runnable objects and associate them with MessageQueue of a thread.
When a Handler instance is created, it is bound to the Message Queue that creates the thread.

First, create a Handler instance in the main thread and bind it to the MessageQueue of the main thread. The MessageQueue contains a Looper, which constantly checks whether the message queue has messages. If there is a message, it will be thrown to Handler's handleMessage for processing.
At this time, the sub-thread obtains the Handler object and sends a message to the MessageQueue of the main thread. The message queue has a message, and the message will be thrown to handleMessage to process the message.
The above process sounds like a loop. Why? Can't I handle it in a child thread?
We know that subthreads are generally used to process time-consuming operations and cannot update the UI.
It is difficult to control the UI because multiple threads are used to perform operations on the UI at the same time. UI operations must be performed by the main thread.

Therefore, we need to update the UI in the Child thread. We can use the Handler provided by android for us. It is mainly used to solve the contradiction that the UI cannot be accessed in the Child thread.
If you forcibly operate the UI in the sub-thread, "android. view. viewRootImpl $ CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views..

1. What did Handler do when it was new Handler?

Lorule. mylorule ()


The logoff object is obtained from the ThreadLocal set get. So when does ThreadLocal set the logoff object?

In the prepare method, the LoZ metadata? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcGVyttTP86OssqLM7bzTtb3By7TmtKLXxUxvb3BlcrbUz/empty" here write picture description "src =" http://www.bkjia.com/uploads/allimg/160407/04145QQ6-5.png "title =" \ "/>
At the same time as the new logoff, The MessageQueue instance is also created.

The prepare method is called preparemainloe.
During application running, the main thread ActivityThread is generated and executed. In the main method of the main thread, prepareMainLooper is called.
That is to say, when the application is started, the logoff and MessageQueue objects will be created. In the Handler constructor, the created logoff and MessageQueue are obtained.

2. Message object
New Message can be used to directly create a Message object.
You can use handler. obtainMessage (there are many overloaded methods) to create a message. You can refer to the source code to know that all the messages are created by the obtain method.

SPool indicates the first Message in the Message pool. If no Message exists in the Message pool, a new Message is returned.
If a message pool exists, it is used up to return messages in the message pool.
Messages in the Message pool are maintained by the Message itself, rather than MessageQueue.
Messages in the message pool are placed as follows:


SPool indicates that the first message is directed to.
Message m=sPool;
At this time, m also points to
sPool=m.next
SPool no longer points to a, but to the next message B.
m.next=null
sPoolSize--
The next member variable of a no longer points to B and is set to null. The length of the message queue is reduced, and the message in the message pool is returned.

3. logoff process for Message Processing
After a subthread sends a message, the MessageQueue of the main thread has a message. When messages are sent to Handler for processing, logoff is required for polling.
Locate the logoff loop method of the polling message.

 Message msg = queue.next(); // might block .......... .......... ..........            msg.target.dispatchMessage(msg);

The message sent from the queue is the dispatchMessage method of the member variable msg.tar get.
Msg.tar get is Handler type. How is msg.tar get passed in:

public static Message obtain(Handler h) {        Message m = obtain();        m.target = h;        return m;    }

The handler is assigned a target of the Handler type when creating a Message.
In dispatchMessage, handleMessage is called.
In the main thread, the handleMessage we define will overwrite this method.
Therefore, the Message object is passed to the handleMessage of the main thread Handler step by step.
The conclusion is: the Handler that sends the message will process the message.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.