从源码与官方文档看之Handler篇(四)

简介: 上回我们说到第一个Handler类的含有控制是否异步执行的构造函数,今天我们继续往下阅读,相信能更清楚的了解Handler类的机制。

正篇

首先我们看一下接下来这个构造函数的注释与代码:

/**
 * Use the {@link Looper} for the current thread with the specified callback interface
 * and set whether the handler should be asynchronous.
 *
 * Handlers are synchronous by default unless this constructor is used to make
 * one that is strictly asynchronous.
 *
 * Asynchronous messages represent interrupts or events that do not require global ordering
 * with respect to synchronous messages.  Asynchronous messages are not subject to
 * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
 *
 * @param callback The callback interface in which to handle messages, or null.
 * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
 * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
 *
 * @hide
 */
public Handler(@Nullable 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为空做异常抛出。 接着我们看下一个,还是构造方法:

/**
 * Use the provided {@link Looper} instead of the default one and take a callback
 * interface in which to handle messages.  Also set whether the handler
 * should be asynchronous.
 *
 * Handlers are synchronous by default unless this constructor is used to make
 * one that is strictly asynchronous.
 *
 * Asynchronous messages represent interrupts or events that do not require global ordering
 * with respect to synchronous messages.  Asynchronous messages are not subject to
 * the synchronization barriers introduced by conditions such as display vsync.
 *
 * @param looper The looper, must not be null.
 * @param callback The callback interface in which to handle messages, or null.
 * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
 * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
 *
 * @hide
 */
@UnsupportedAppUsage
public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) {
    mLooper = looper;
    mQueue = looper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}

从注释来看,这个构造方法和之前带是否异步参数的方法一样,可以去看一下我之前写的那一篇。

(未完待续)

相关文章
|
消息中间件 Android开发
Handler源码解读——handler使用时的注意事项
工作中经常会遇到从子线程发送消息给主线程,让主线程更新UI的操作,常见的有handler.sendMessage(Message),和handler.post(runnable)和handler.postDelayed(runnable, milliseconds);一直在使用这些方法,却不知道他们的原理,今天就来解释一下他们的原理。
|
消息中间件 Android开发
从源码与官方文档看之Handler篇(九)
今天我们来详细看看Handler的post()方法吧
|
Java Android开发
从源码与官方文档看之Handler篇(二)
今天又发现自己原来是Handler这个类名打错了导致前面几篇一直再说Java的Handle,而安卓的是Handler,前一篇文章我们主要翻译了安卓官方对Handler的注释,这一篇我们来看看Handler类的一些属性,成员变量。
从源码与官方文档看之Handler篇(七)
接下来就是我们可以正常调用的Handler类方法了,希望早日完成Handler类的源码阅读。
|
安全 API 调度
从源码与官方文档看之Handler篇(十)
每次阅读源码,我都想着许多过往云烟,可以说,一切不在一样,不过,想再多又如何,倒不如按下心思,在手中记下这一切更为妥当。还是看看这些充满智慧结晶的源码吧!
从源码与官方文档看之Handler篇(五)
尽管前路坎坷,可我们还是继续走了下去,一转眼已经又到了第五篇了,每篇内容较少,所以后面整理完时,会汇总成一篇文章,相信这样做会更具有可读性。
|
安全 API
从源码与官方文档看之Handler篇(六)
今天继续看Handler类,构造方法已经基本全部看完,后面应该是一些类的一般方法之类的内容了,继续加油。
|
消息中间件 Java 调度
从源码与官方文档看之Handler篇(一)
好家伙,写了四篇文章才发现自己看源码的Handle所属的包不一样
|
Android开发
从源码与官方文档看之Handler篇(八)
后面更文节奏可以舒缓一些了,每篇文章的篇幅也会长些许。 废话不多说,我们继续看Handler类。
从源码与官方文档看之Handler篇(三)
前面我们说到dispatchMessage方法,今天我们继续往下看源码。
105 0