Links between Android hander, Looper, and message

Source: Internet
Author: User

1. First Looper.prepare () saves an looper instance in this thread, and then holds a MessageQueue object in the instance, because Looper.prepare () can only be called once in a thread. So MessageQueue will only exist in one thread.
2. Looper.loop () causes the current thread to enter an infinite loop, which reads the message from the MessageQueue instance and then callbacks the Msg.target.dispatchMessage (msg) method.
3, handler the construction method, will first get the current thread saved Looper instance, and Looper instance in the MessageQueue want to associate.
4, Handler SendMessage method, will give the target of MSG to handler itself, and then add MessageQueue.
5. When constructing handler instances, we override the Handlemessage method, which is the method that Msg.target.dispatchMessage (msg) eventually calls.
In activity, we did not show calls to the Looper.prepare () and Looper.loop () methods, why handler can be created successfully,
This is because in the activity's startup code, the Looper.prepare () and Looper.loop () methods have been called in the current UI thread

Produces a message object that can be new or use the Message.obtain () method;

However, it is more recommended to use the obtain method, because the message internally maintains a message pool for reuse of message, and avoids using new to reallocate memory.

Reference: Android handlerthread Full parsing

Links between Android hander, Looper, and message

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.