android UI线程向子线程发送Message_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > android UI线程向子线程发送Message

android UI线程向子线程发送Message

 2010/11/19 9:20:01  亚当爱上java  http://zhangkun716717-126-com.javaeye.com  我要评论(0)
  • 摘要:在android中一般是子线程向主线程发送消息,那主线程能否向子线程发送消息呢?答案是肯定的。请看android文档中Looper类的一段文档:Classusedtorunamessageloopforathread.Threadsbydefaultdonothaveamessageloopassociatedwiththem;tocreateone,callprepare()inthethreadthatistoruntheloop,andthenloop(
  • 标签:androidUI线程向子线程发送Message
在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。先写到这里吧,等周末有空再进行详细说明。
  • 相关文章
发表评论
用户名: 匿名