在android中一般是子
线程向主线程发送消息,那主线程能否向子线程发送消息呢?答案是肯定的。
请看android文档中Looper类的一段文档:
Class used to run a message loop for a
thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the implementation of a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.
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();
}
}
主线程通过子线程中定义的Handler 向子线程发送message。先写到这里吧,等周末有空再进行详细说明。