Handler Looper MessageQueue 之间的关系
2015-09-25 11:08
501 查看
Handler Looper MessageQueue 之间的关系
handler在安卓开发中常用于更新界面ui,以及其他在主线程中的操作。内部结构大概图为:
1、handler持有一个Looper对象,这个Looper对象可以是自定义子线程的Looper,也可以是默认MainLooper。Looper主要作用就是不断循环MessageQueue中的Message,回调Handler的dispatchMessage方法。源码:
2、Handler还持有MessageQueue对象的引用,用于sendMessage时,将消息添加到MessageQueue中。这样Looper就可以获取到最新的message。Handler中的MessageQueue其实就是Looper的MessageQueue成员属性。
所以一个大致的流程就是:
1、Looper在在不断的获取MessageQueue消息,调用handler的dispatchMessage方法
2、Handler.postMessage就是往Looper的MessageQueue中添加Message,并且设置message.target为Handler本身
handler在安卓开发中常用于更新界面ui,以及其他在主线程中的操作。内部结构大概图为:
1、handler持有一个Looper对象,这个Looper对象可以是自定义子线程的Looper,也可以是默认MainLooper。Looper主要作用就是不断循环MessageQueue中的Message,回调Handler的dispatchMessage方法。源码:
final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } //这里回调handler中的dispatchMessage方法 msg.target.dispatchMessage(msg); if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked(); }
2、Handler还持有MessageQueue对象的引用,用于sendMessage时,将消息添加到MessageQueue中。这样Looper就可以获取到最新的message。Handler中的MessageQueue其实就是Looper的MessageQueue成员属性。
public Handler(Looper looper, Callback callback, boolean async) { mLooper = looper; mQueue = looper.mQueue; mCallback = callback; mAsynchronous = async; }
所以一个大致的流程就是:
1、Looper在在不断的获取MessageQueue消息,调用handler的dispatchMessage方法
2、Handler.postMessage就是往Looper的MessageQueue中添加Message,并且设置message.target为Handler本身
相关文章推荐
- easyui tree 取消选中节点
- ios9兼容系列之解决iPad提交报错“iPad Multitasking support requires launch story board in bundle '”
- maven+liquibase实现持续数据库集成
- 黑马程序员--GUI
- Android ViewGroup事件分发机制(requestDisallowInterceptTouchEvent方法)
- Required CommonsMultipartFile parameter 'pic' is not present
- UITextView如何设置提示性文字
- 双端队列 HDOJ 3530 Subsequence
- VMware Workstation 11启用UEFI BIOS
- FineUI简介
- iOS初始化UIView
- 黑马程序员学习(十一)GUI学习
- 深入讲解iOS开发中的UIViewController
- iOS初始化UIWindow并且设置级别
- UIView的扩大,缩放,旋转,平移,反转等效果
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
- UITableView自动调整Cell高度
- 浅谈UITableview 基本使用
- UIView适应内容
- BFS HDOJ 1242 Rescue