Recently found some information about the communication between Android threads, organized and learned a bit, and made a simple example.
Andriod provides Handler and Looper to meet the communication between threads. For example, a sub-thread downloads a picture from the network and sends a message to the main thread when it is downloaded, and this message is passed by binding to the handler of the main thread.
on Android, where threads are divided into message loop threads and no message loops, threads that have a message loop will generally have a new concept of looper, the thing about Android. Our main thread (the UI thread) is the one that threads the message loop. For this kind of message loop mechanism, we introduce a new mechanism handle, we have the message loop, we must send the corresponding message in the message loop, the custom message will have its own corresponding processing, the message is sent and purged, the processing of the message, these are encapsulated in the handle inside, Note that handle is only for those threads that have looper, whether it's the UI thread or the child thread, as long as you have looper, I can add something to your message queue and handle it accordingly. But here's another point, as long as it is about UI-related things, it cannot be placed in a child thread, because the child thread cannot manipulate the UI, only the data, the system and other non-UI operations.
On Android, where threads are divided into message loops and no message loops, threads with message loops generally have a looper, a new concept for Android. Our main thread (the UI thread) is the one that threads the message loop. For this kind of message loop mechanism, we introduce a new mechanism handler, we have the message loop, we must send the corresponding message in the message loop, the custom message will have its own corresponding processing, the message is sent and cleared, these are encapsulated in the handler inside, Note that handler is only for those threads that have looper, whether it's the UI thread or the child thread, as long as you have looper, I can add something to your message queue and handle it accordingly.
But here's another point, as long as it is about UI-related things, it cannot be placed in a child thread, because the child thread cannot manipulate the UI, only the data, the system and other non-UI operations.
The creation of a handler will be bound to the message queue of this thread, if it is created on the primary thread, there is no need to write code to create the message queue, the default message queue will be created on the main thread. However, if you are on a child thread, you must initialize the thread's message queue before creating the handler. As in the following code:
?
| 123456789Ten One A - - the - - - + - + A at - - - - - in |
class ChildThread extends Thread { public void run() { /* * 创建 handler前先初始化Looper. */ Looper.prepare(); /* * 在子线程创建handler,所以会绑定到子线程的消息队列中 * */ mChildHandler = new Handler() { public void handleMessage(Message msg) { /* * Do some expensive operations there. */ } }; /* * 启动该线程的消息队列 */ Looper.loop(); }} |
When handler receives the message, it runs handlemessage (...). callback function, you can do some time-consuming operations inside.
Finally completes the operation to end the child thread, remember to call quit () to end the message loop queue.
Android Inter-thread communication