前言

提到Handler大家应该都不会陌生,它是安卓中消息机制的主要核心类,配合MessageQueueLooper一起使用构成了我们所熟悉的Android消息机制。Handler的主要工作是将一个任务切换到指定的线程去执行,因为Android中规定访问UI只能在主线程中进行,如果在子线程中访问UI,会抛出异常。而Android中不建议在主线程执行耗时操作否则会造成ANR,所以说。系统提供Handler的主要原因是为了解决在子线程中无法访问UI的问题。

本文深入分析 Android 的消息处理机制,了解 Handler 的工作原理。

Handler

创建一个handler的构造方法很多,不过最终都会调用下面这两个构造方法,贴源码:

    public Handler(Looper looper, Callback callback, boolean async) {        mLooper = looper;        mQueue = looper.mQueue;        mCallback = callback;        mAsynchronous = async;    }    public Handler(Callback callback, boolean async) {        if (FIND_POTENTIAL_LEAKS) {            final Class<? extends Handler> klass = getClass();            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&                    (klass.getModifiers() & Modifier.STATIC) == 0) {                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +                    klass.getCanonicalName());            }        }        mLooper = Looper.myLooper();        if (mLooper == null) {            throw new RuntimeException(                "Can't create handler inside thread " + Thread.currentThread()                        + " that has not called Looper.prepare()");        }        mQueue = mLooper.mQueue;        mCallback = callback;        mAsynchronous = async;    }

可以看到,主要完成了 Looper mLooper,MessageQueue mQueue,Callback mCallback 和 boolean mAsynchronous的初始化。通过调用 Looper.myLooper() 获得了 Looper 对象。如果 mLooper 为空,那么会抛出异常:”Can’t create handler inside thread that has not called Looper.prepare()”,意思是:不能在未调用 Looper.prepare() 的线程创建 handler

Looper

1.在 Handler 的构造方法中调用 Looper.myLooper() 获得了 Looper 对象,方法源码如下:

    // sThreadLocal.get() will return null unless you've called prepare().    static final ThreadLocal sThreadLocal = new ThreadLocal();    public static @Nullable Looper myLooper() {        return sThreadLocal.get();    }

从 sThreadLocal 中获取了Looper对象,ThreadLocal是一个线程内部的数据存储类,通过它可以在指定线程中存储数据,存储后可以在指定线程中获取到存储的数据,而其他线程则无法获取到数据。这里用ThreadLocal说明Looper是线程独立的。

2.在Handler的构造方法中异常信息可知道如果没有调用Looper.prepare()是不能创建handler的,因为并没有获取Lopper,源码如下。

    public static void prepare() {        prepare(true);    }    private static void prepare(boolean quitAllowed) {        if (sThreadLocal.get() != null) {            throw new RuntimeException("Only one Looper may be created per thread");        }        sThreadLocal.set(new Looper(quitAllowed));    }

调用了Looper的构造方法创建了Looper对象,然后设置给sThreadLocal,这里需要注意如果已经设置过就会抛出异常,因为一个线程只会有一个Looper。下面看Looper构造方法:

    private Looper(boolean quitAllowed) {        mQueue = new MessageQueue(quitAllowed);        mThread = Thread.currentThread();    }

在这里创建了MessageQueue消息队列对象,获取了当前线程。

3.有了Looper之后,还必须调用Looper.loop()才能开启消息轮询,这样才能不断从消息队列 MessageQueue 取出消息交由 Handler 处理。

    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();        // Allow overriding a threshold with a system prop. e.g.        // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'        final int thresholdOverride =                SystemProperties.getInt("log.looper."                        + Process.myUid() + "."                        + Thread.currentThread().getName()                        + ".slow", 0);        boolean slowDeliveryDetected = false;        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            final Printer logging = me.mLogging;            if (logging != null) {                logging.println(">>>>> Dispatching to " + msg.target + " " +                        msg.callback + ": " + msg.what);            }            final long traceTag = me.mTraceTag;            long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;             //......省略不关键代码            try {                msg.target.dispatchMessage(msg);                dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;            } finally {                if (traceTag != 0) {                    Trace.traceEnd(traceTag);                }            }            if (logSlowDelivery) {                if (slowDeliveryDetected) {                    if ((dispatchStart - msg.when) <= 10) {                        Slog.w(TAG, "Drained");                        slowDeliveryDetected = false;                    }                } else {                    if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",                            msg)) {                        // Once we write a slow delivery log, suppress until the queue drains.                        slowDeliveryDetected = true;                    }                }            }            if (logSlowDispatch) {                showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", 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();        }    }

这里主要是一直循环调用queue.next()拿到Message,然后通过msg.target.dispatchMessage(msg)方法处理,target是发送这条消息的handler对象(下面的分析会看到),这样交给handler的dispatchMessage(msg)处理。该循环只会在queue.next()为null时,才能跳出循环,否则一直阻塞下去。

MessageQueue

MessageQueue主要包含两个操作:插入和读取。读取的时候伴随着删除操作。

插入操作:enqueueMessage(),实现单链表的插入操作。

    boolean enqueueMessage(Message msg, long when) {        synchronized (this) {            msg.markInUse();            msg.when = when;            Message p = mMessages;            boolean needWake;            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 {                // 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;    }

读取操作:next()方法,代码较长,看看就行了。无线循环,这个方法被Looper.loop()调用。

    Message next() {        final long ptr = mPtr;        if (ptr == 0) {            return null;        }        int pendingIdleHandlerCount = -1; // -1 only during first iteration        int nextPollTimeoutMillis = 0;        for (;;) {            if (nextPollTimeoutMillis != 0) {                Binder.flushPendingCommands();            }            nativePollOnce(ptr, nextPollTimeoutMillis);            synchronized (this) {                // Try to retrieve the next message.  Return if found.                final long now = SystemClock.uptimeMillis();                Message prevMsg = null;                Message msg = mMessages;                if (msg != null && msg.target == null) {                    // Stalled by a barrier.  Find the next asynchronous message in the queue.                    do {                        prevMsg = msg;                        msg = msg.next;                    } while (msg != null && !msg.isAsynchronous());                }                if (msg != null) {                    if (now < msg.when) {                        // Next message is not ready.  Set a timeout to wake up when it is ready.                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);                    } else {                        // Got a message.                        mBlocked = false;                        if (prevMsg != null) {                            prevMsg.next = msg.next;                        } else {                            mMessages = msg.next;                        }                        msg.next = null;                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);                        msg.markInUse();                        return msg;                    }                } else {                    // No more messages.                    nextPollTimeoutMillis = -1;                }                // Process the quit message now that all pending messages have been handled.                if (mQuitting) {                    dispose();                    return null;                }                // If first time idle, then get the number of idlers to run.                // Idle handles only run if the queue is empty or if the first message                // in the queue (possibly a barrier) is due to be handled in the future.                if (pendingIdleHandlerCount < 0                        && (mMessages == null || now < mMessages.when)) {                    pendingIdleHandlerCount = mIdleHandlers.size();                }                if (pendingIdleHandlerCount <= 0) {                    // No idle handlers to run.  Loop and wait some more.                    mBlocked = true;                    continue;                }                if (mPendingIdleHandlers == null) {                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];                }                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);            }            // Run the idle handlers.            // We only ever reach this code block during the first iteration.            for (int i = 0; i < pendingIdleHandlerCount; i++) {                final IdleHandler idler = mPendingIdleHandlers[i];                mPendingIdleHandlers[i] = null; // release the reference to the handler                boolean keep = false;                try {                    keep = idler.queueIdle();                } catch (Throwable t) {                    Log.wtf(TAG, "IdleHandler threw exception", t);                }                if (!keep) {                    synchronized (this) {                        mIdleHandlers.remove(idler);                    }                }            }            // Reset the idle handler count to 0 so we do not run them again.            pendingIdleHandlerCount = 0;            // While calling an idle handler, a new message could have been delivered            // so go back and look again for a pending message without waiting.            nextPollTimeoutMillis = 0;        }    }

再回到Handler

Handler主要负责消息的发送和接收,发送可以通过一系列post方法和send方法,post方法最终也会调用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);    }    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

之前在Looper介绍过,在loop方法中调用MessageQueue.next()方法拿到msg后,会调用handler的dispatchMessage()方法,dispatchMessage方法实现如下

   public void dispatchMessage(Message msg) {        if (msg.callback != null) {            handleCallback(msg);        } else {            if (mCallback != null) {                if (mCallback.handleMessage(msg)) {                    return;                }            }            handleMessage(msg);        }    }

这里的msg.callback就是一个Runnable对象,也就是Handler.post()传递的参数。

    private static void handleCallback(Message message) {        message.callback.run();    }

mCallback是一个Callback接口,定义如下:

    /**     * Callback interface you can use when instantiating a Handler to avoid     * having to implement your own subclass of Handler.     */    public interface Callback {        /**         * @param msg A {@link android.os.Message Message} object         * @return True if no further handling is desired         */        public boolean handleMessage(Message msg);    }

作用和它的注释一样,可以用来创建一个Handler的实例,但并不需要派生Handler的子类。在Handler构造方法中讲过,可以传一个Callback进去,这样就不用重写Handler的handleMessage方法。配张图来解释下dispatchMessage流程。Android Handler 的工作原理解析_第1张图片

总结

现在我们可以知道消息处理是通过 Handler 、Looper 以及 MessageQueue共同完成。 Handler 负责发送以及处理消息,Looper 创建消息队列并不断从队列中取出消息交给 Handler, MessageQueue 则用于保存消息。这三者是一个整体,缺一不可。

更多相关文章

  1. Android原生Switch控件滑块thumb卡住问题的解决方法
  2. Android开发实践:基于命令模式的异步任务线程
  3. Android 静默安装和智能安装的实现方法
  4. 学习Android线程模型解析(包括UI的更新)
  5. Android Studio——为什么说android UI操作不是线程安全的
  6. .net平台借助第三方推送服务在推送Android,IOS消息(极光推送_V2版
  7. Android中Handler的使用方法——在子线程中更新界面
  8. android 使用asynctask结合fragment更新UI(另附线程池管理示例)
  9. Android的消息机制——Handler的工作过程

随机推荐

  1. 如何使用JavaScript修改基于onclick的文
  2. vue常见面试题
  3. pdf2htmlEX 安装与保持最新版本
  4. html5在pc能实现下拉菜单,在手机实现不了
  5. 开源项目Html Agility Pack实现快速解析H
  6. C 实现HTML5服务时,遇到握手状态的判断问
  7. 100%高度分区内显示:表格单元分区
  8. 我使用生成html文件的python制作了一个脚
  9. HTML代码格式化工具
  10. html5 canvas实例 绘制变形图形 径向渐变