android UI线程向子线程发送Message
16lz
2021-12-04
在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中对Handle机制的理解
- Android(安卓)Handler详解
- Android(安卓)异步加载解决方案(循环,多任务)
- Android的子线程能更新UI吗?
- Android(安卓)回炉 阿里巴巴Android开发规范
- android 将程序设置为app2sd
- Android异步加载图像小结
- Android中Handler源码解析(一)
- android 线程大集合