在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.
主线程通过子线程中定义的Handler 向子线程发送message。先写到这里吧,等周末有空再进行详细说明。
请看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。先写到这里吧,等周末有空再进行详细说明。
本文探讨了Android应用程序中主线程与子线程之间的消息传递机制。具体介绍了如何利用Looper和Handler类实现主线程向子线程发送消息的过程,并提供了一个简单的Looper线程实现示例。
208

被折叠的 条评论
为什么被折叠?



