Handler Message Looper

Handler的创建需要绑定一个Looper对象和Looper对象的MessageQueen,默认调用mLooper = Looper.myLooper(),会返回当前线程中的Looper对象。

/**

* Return the Looper object associated with the current thread.  Returns

* null if the calling thread is not associated with a Looper.

*/

public static @Nullable Looper myLooper() {

return sThreadLocal.get();

}

Handler发送消息到MessageQueen中,Looper对象取出MessageQueen中的meesage,发送给Handler处理。

for (;;) {

Message msg = queue.next(); // might block

msg.target.dispatchMessage(msg);

}

创建looper sample

*  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();

*      }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容