Android refers to the Message Loop Mechanism of Windows to realize the message loop of Android.
Android uses logoff and Handler to implement the message loop mechanism. Android message loops are targeted at threads (each thread can have its own message queue and message loop ).
In Android, logoff is responsible for managing the message queues and message loops of threads. We can use Loop. myLooper () to obtain the Looper object of the current thread, and use Loop. getMainLooper () to obtain the Looper object of the main thread of the current process.
A thread can exist (or does not exist), a message queue, and a message loop ).
An Activity is a UI thread that runs in the main thread. When the Android system starts the Activity, it creates a message queue and a message loop ).
Handler is used to add a message to a specific logoff message queue and distribute and process the messages in the queue. When constructing a Handler, you can specify a logoff object. If this parameter is not specified, the logoff object of the current thread is used.
Shows the relationship between Activity, logoff, Handler, and Thread:
Multiple worker threads or other components can be created in an Activity. If these threads or components put their messages into the active thread message queue of the Activity, the message will be processed in the main thread.
The main thread is generally responsible for interface update operations, and the widgets in html "target = _ blank> Android system are not thread-safe. Therefore, this method can be used to update the Android interface. This method is widely used in Android systems.
So how does one thread put messages 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 Logoff of the main thread, calling the sendMessage and Other interfaces of Handler will put all messages in the queue into the message queue of the main thread. In addition, the handleMessage interface of the Handler will be called in the main thread of the handler to process the message.
For more information about Android message queue, see: http://my.unix-center.net /~ Simon_fu /? P = 652
The following figure illustrates their relationship from another perspective:
References:
Android asynchronous loading image Summary: http://www.bkjia.com/kf/201105/89970.html
Deep understanding of Android Message Processing System-lofter, Handler, Thread: http://www.bkjia.com/kf/201105/89971.html
Android thread model (Painless Threading): http://www.bkjia.com/kf/201105/89972.html
Android thread controls UI Update (Handler, post (), postDelayed (), postAtTime)
Http://www.bkjia.com/kf/201105/89973.html
Android-Multithreading in a UI environment
Http://www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/
Handler, lofter, MessageQueue and Thread: http://www.bkjia.com/kf/201105/89903.html in Android
Android Runnable: http://www.bkjia.com/kf/201105/89974.html