| www.sharp-i.net |
|
|||||||
|
![]() |
|||
|
|
Introduce Android message dispose system principle:Addtime:2011-12-08 Editor:-- Guangzhou Sharp Eye Digital Technology Co., Ltd. Is the professional supplier of embedded and streaming media system solution. The company is engaged in embedded developing for so many years with rich experience in embedded Linux, devote to the develop streaming media system solution based on Linux, has already developed various sets products related to media system. Meanwhile, we offer all kinds of network advertising player and kiosk player and so on. There is a professional, fast response, flexible developing group in our company which can quickly offer OEM service for customers based on our current technology. The following let us take this opportunity to introduce the Android message dispose system principle:
Looper of Android is responsible for managing thread’s message queue and message circulate, about the detail please refer to the Looper’s sound code. You can get the current thread’s Looper target through Loop.myLooper(),through Loop.getMainLooper() can get the current course’s main thread Looper object.
The previous we mentioned that Android system’s message queue and message circulate are aim at specific thread, one thread can exist ( of course can be inexistence either) one message queue and one message circulate (Looper), special thread’s message only can be sent to its thread, can't cross-cultural thread and cross-cultural thread communication. But creating the work is no news cycle thread default and the message queue, if want to let this thread with the message queue and news cycle, firstly you need to invoke Looper.prepare in the thread to creat message queue, and then invoke Looper.loop() to enter into message circulate. For example:
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
So your thread have message dispose mechanism, in the Handler of message handling. Activity is a UI thread, run on main thread, Android system in start-up time for Activity will create a message queue and news cycle (Looper). About detail please refer to ActivityThread.java file.
The function of Handler is put the message into specific (Looper)message queue, hand out and dispose the message in this message queue. When structuring Handler can assign a Looper object, if not can make use of current thread’s Looper to creat.
One Activity can create various work thread or the other assembly, If these threads or components put their news in Activity of the main thread the message queue, then this message will be disposed in the main thread. Because main thread usually responsible for the update operate of the interface, and Android system’s weget is not safe thread, so this way can better realize Android interface’s update. This way is widely used in Android system. How can another thread put the message into the message queue of the main thread ? The answer is through Handle object. As long as the Handler objects to create the main thread Looper, then invoke Handler's sendMessage interface etc, Will the message in a queue is put in the thread message queue. And will in the main thread of Handler invoke the Handler of handleMessage interface to deal with the messages.
|
||||
![]() |
2th Floor, 11th East building, Huangzhou Industrial Park,Chebei Road, Tinahe District,Guangzhou |