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

简介: 前面我们说到dispatchMessage方法,今天我们继续往下看源码。

正篇

首先后面有两个抛弃的方法:

@Deprecated
public Handler() {
    this(null, false);
}
@Deprecated
public Handler(@Nullable Callback callback) {
    this(callback, false);
}

这两个默认构造方法因为不安全,它们与当前线程的 Looper 相关联,而如果该线程没有 Looper,则该处理程序将无法接收消息,从而会引发异常。 接着我们继续看其他构造方法:

/**
 * Use the provided {@link Looper} instead of the default one.
 *
 * @param looper The looper, must not be null.
 */
public Handler(@NonNull Looper looper) {
    this(looper, null, false);
}
/**
 * Use the provided {@link Looper} instead of the default one and take a callback
 * interface in which to handle messages.
 *
 * @param looper The looper, must not be null.
 * @param callback The callback interface in which to handle messages, or null.
 */
public Handler(@NonNull Looper looper, @Nullable Callback callback) {
    this(looper, callback, false);
}

其中第一个构造函数是使用提供的Looper而不是默认的,且参数looper不能为空; 而第二个构造函数与第一个类似,不过增加了一个回调接口来处理消息,该回调接口可以为null

这里要注意的是这里调用的this()方法中的boolean参数是用来判断是否需要异步处理消息,后面的其他构造函数注释中有相应的解释。

我们接着往下看:

/**
 * Use the {@link Looper} for the current thread
 * 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 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(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public Handler(boolean async) {
    this(null, async);
}

这个构造方法是为当前线程使用 Looper 并设置handler是否应该是异步的。我们在默认情况下,Handlers是同步的,除非使用此构造函数来创建一个严格异步的Handler。注释翻译说: 异步消息表示不需要对同步消息进行全局排序的中断或事件,而且异步消息不受 MessageQueue.enqueueSyncBarrier(long) 引入的同步屏障的影响。

参数:async – 如果为 true,则处理程序为发送给它的每个 Message 或发布给它的 Runnable 调用 Message.setAsynchronous(boolean)。

(未完待续)

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