Android Development note "Android Message Queuing model"

Source: Internet
Author: User

Android is referring to the Windows message loop mechanism to implement Android's own message loop.
Android uses Looper, handler to implement the message loop mechanism, the Android message loop is for threads (each thread can have its own message queue and message loop).
In Android, Looper is responsible for managing thread Message Queuing and message loops. We can get the Looper object of the current thread through Loop.mylooper (), and the Looper object of the current process's main thread can be obtained through Loop.getmainlooper ().
A thread can exist (and, of course, not exist) a message queue and a message loop (Looper).
Activity is a UI thread that runs in the main thread, and the Android system creates a message queue and message loop (Looper) for activity when it starts.
The role of handler is to add messages to a specific (Looper) message queue and to distribute and process messages in that message queue. When constructing handler, you can specify a Looper object that is created with the looper of the current thread if not specified.
The relationship between Activity, Looper, and Handler,thread is as follows:

One activity can create multiple worker threads or other components, and if those threads or components put their messages into the activity's main thread message queue, the message will be processed in the main thread.

Because the main thread is generally responsible for updating the interface, and the widgets in the Android system are not thread-safe, this is a good way to implement the Android interface update. This approach is widely used in Android systems.

So how does a thread put the message into the message queue of the main thread? The answer is through the handle object, as long as the handler object is created with the looper of the main thread, then invoking the SendMessage and other interfaces of the handler will put the message into the queue will be put into the main thread of the message queue. The handler's Handlemessage interface will be called in the handler main thread to process the message.

For more information on Android Message Queuing, see: http://my.unix-center.net/~Simon_fu/?p=652

The following diagram depicts their relationship from a different perspective:

Original site: http://blog.csdn.net/libaohan/article/details/7287657

Resources:

Android Asynchronous Load Image summary
Http://blog.csdn.net/sgl870927/archive/2011/03/29/6285535.aspx

Deep understanding of Android Message processing system--looper, Handler, Thread

http://my.unix-center.net/~Simon_fu/?p=652

Android Threading Model (painless threading)
http://android.group.iteye.com/group/blog/382683

Android Threading Controls UI updates (Handler, post (), postdelayed (), Postattime)
Http://lepeng.net/blogger/?p=21

Android–multithreading in a UI environment
http://www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/

Handler, Looper, MessageQueue and thread in Android
Http://www.cnblogs.com/xirihanlin/archive/2011/04/11/2012746.html

Android Runnable
Http://blog.csdn.net/michaelpp/archive/2010/06/30/5704682.aspx

Android Development note "Android Message Queuing model"

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.