handler:message:looper机制
2016-02-23 22:39
393 查看
Handler/Message/Looper机制
Handler作用
android开发过程中,我们都知道在一个异步线程中完成一段耗时操作,利用handler把结果send到UI主线程,从而更新ui;Android应用在初始化启动时,在ActivityThread中也定义了一个Handler的派生类H,用来进行Activity和Service生命周期的管理等;private class H extends Handler { ...//省略 public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what)); switch (msg.what) { case LAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); final ActivityClientRecord r = (ActivityClientRecord) msg.obj; r.packageInfo = getPackageInfoNoCheck( r.activityInfo.applicationInfo, r.compatInfo); handleLaunchActivity(r, null); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; case RELAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart"); ActivityClientRecord r = (ActivityClientRecord)msg.obj; handleRelaunchActivity(r); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; case PAUSE_ACTIVITY: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause"); handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2, (msg.arg1&2) != 0); maybeSnapshot(); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; ... } if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what)); } ...//省略 }
Message的send机制
public final boolean sendMessage(Message msg) { return sendMessageDelayed(msg, 0); }
往下追踪
public final boolean sendMessageDelayed(Message msg, long delayMillis) { if (delayMillis < 0) { delayMillis = 0; } return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); }
继续往下追踪
public boolean sendMessageAtTime(Message msg, long uptimeMillis) { MessageQueue queue = mQueue; if (queue == null) { RuntimeException e = new RuntimeException( this + " sendMessageAtTime() called with no mQueue"); Log.w("Looper", e.getMessage(), e); return false; } return enqueueMessage(queue, msg, uptimeMillis); }
这个方法中我们大致可以看出消息Message是进入一个队列mQueue当中,mQueue是Handler实例化时,从UI主线程里面的Looper中获取到的
public Handler(Callback callback, boolean async) { ...//省略 mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = callback; mAsynchronous = async; }
每个线程只能对应一个Looper,UI主线程中的Looper在应用中是全局的,所有Message发送后都存储在这个Looper的MessageQueue中
继续往下看
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } return queue.enqueueMessage(msg, uptimeMillis); }
以上这些都发生在Handler中,下面进入MessageQueue,看看消息Message是如何进入队列的
boolean enqueueMessage(Message msg, long when) { //处理消息msg的Handler不能为空,所有的消息最终还是交给Handler处理的 if (msg.target == null) { throw new IllegalArgumentException("Message must have a target."); } //如果消息msg正在使用中,无需继续执行下去,同时抛出一个异常 if (msg.isInUse()) { throw new IllegalStateException(msg + " This message is already in use."); } synchronized (this) { //判断MessageQueue是否已停止,停止抛出一个所在线程死亡的异常 if (mQuitting) { IllegalStateException e = new IllegalStateException( msg.target + " sending message to a Handler on a dead thread"); Log.w(TAG, e.getMessage(), e); msg.recycle(); return false; } //标志这个消息正在使用中 msg.markInUse(); msg.when = when; Message p = mMessages; boolean needWake; //如果在队列中msg前面没有其他消息或者msg不是最新的消息,把当前msg放入队列的头部,如果队列是阻塞状态就唤醒队列 if (p == null || when == 0 || when < p.when) { // New head, wake up the event queue if blocked. msg.next = p; mMessages = msg; needWake = mBlocked; } else {//找到合适的位置把msg放入到队列中 // Inserted within the middle of the queue. Usually we don't have to wake // up the event queue unless there is a barrier at the head of the queue // and the message is the earliest asynchronous message in the queue. needWake = mBlocked && p.target == null && msg.isAsynchronous(); Message prev; for (;;) { prev = p; p = p.next; //队列中没有更多消息,跳出死循环 if (p == null || when < p.when) { break; } if (needWake && p.isAsynchronous()) { needWake = false; } } msg.next = p; // invariant: p == prev.next prev.next = msg; } // We can assume mPtr != 0 because mQuitting is false. if (needWake) { nativeWake(mPtr); } } return true; }
Handler是如何拿到Message的
应用在启动初始化时,在ActivityThread中开启了一个无限循环检查的操作,当有新的Message时,就从消息队列中MessageQueue中拿出来public static void loop() { final Looper me = myLooper(); if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } 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(); // 有可能阻塞 if (msg == null) { // msg为空说明队列已经停止不可用,不执行后面的操作了 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); } //把消息msg交给它的目标Handler来分配处理消息,消息在哪个页面中处理就是通过Message中的target来确定是哪个Activity中的Handler,从而在正确页面中处理msg 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(); } }
Handler调用dispatchMessage(msg)来处理消息
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
我们终于看到熟悉的handleMessage()了
Handler消息机制UML图
相关文章推荐
- linux 常见测试题
- 深入理解Apache Flink核心技术
- Web服务器父与子 Apache和Tomcat区别
- 安装dede UTF_8时报出了一个致命错误和警告,最后不能显示网站后台和首页了
- 如何将CentOS中的中文语言改成英文的
- Linux 学习笔记
- Centos7安装Scrapy过程
- 2016新年Bash的CVE-2014-6271漏洞修复经历
- Centos下安装Scrapy
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter1 Secondary Sort
- 博客网站的安全运维尝试
- 网易公共课Linux Shell脚本应用学习笔记
- 关于opencv使用淘宝视频采集卡蓝屏的办法~
- Linux 的多线程编程的高效开发经验
- Linux时间函数之gettimeofday()函数之使用方法
- 防止自己网站被拿来钓鱼
- 高并发高负载系统架构-php篇
- Tomcat输出catalina.out的大小控制
- 一张图看清Linux 内核运行原理
- linux 安装svn