[Android进阶】Handler机制原理解析

}

mQueue = mLooper.mQueue;//通过Looper对象获取消息队列

mCallback = callback;

mAsynchronous = async;

}

//获取Looper对象

public final Looper getLooper() {

return mLooper;

}

从Handler的构造函数中我们可以发现,Handler在初始化的同时会通过Looper.getLooper()获取一个Looper对象,并与Looper进行关联,然后通过Looper对象获取消息队列。那我们继续深入到Looper源码中去看Looper.getLooper()是如何实现的。

//初始化当前线程Looper

public static void prepareMainLooper() {

prepare(false);

synchronized (Looper.class) {

if (sMainLooper != null) {

throw new IllegalStateException(“The main Looper has already been prepared.”);

}

sMainLooper = myLooper();

}

}

//为当前线程设置一个Looper

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));

}

从上面的程序可以看出通过prepareMainLooper(),然后调用 prepare(boolean quitAllowed)方法创建了一个Looper对象,并通过sThreadLocal.set(new Looper(quitAllowed))方法将该对象设置给了sThreadLocal。

//通过ThreadLocal获取Looper

public static @Nullable Looper myLooper() {

return sThreadLocal.get();

}

通过Looper中的预备工作,sThreadLocal中已经存储了一个Looper对象,然后myLooper()方法通过sThreadLocal.get()方法获取到了Looper。那么消息队列就与线程关联上了,所以各个线程只能访问自己的消息队列。

综上所述,我们可以发现消息队列通过Looper与线程关联上了,而Looper又与Handler是关联的,所以Handler就跟线程、线程的消息队列关联上了。

3.2 如何执行消息循环?

在创建Looper对象后,通过Handler发来的消息放在消息队列中后是如何被处理的呢?这就涉及到了消息循环,消息循环是通过Looper.loop()方法来建立的。源代码如下:

//执行消息循环

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;//获取消息队列

//代码省略

for (;😉 {//死循环

Message msg = queue.next(); // 获取消息

if (msg == null) {

// No message indicates that the message queue is quitting.

return;

}

//代码省略

try {

msg.target.dispatchMessage(msg);//分发消息

} finally {

if (traceTag != 0) {

Trace.traceEnd(traceTag);

}

}

//代码省略

msg.recycleUnchecked();//消息回收

}

}

从源码中我们可以看出,loop()方法实质上就是通过一个死循环不断的从消息队列中获取消息,然后又不断的处理消息的过程。

3.3 消息处理机制

msg.target.dispatchMessage(msg);//分发消息

我们从loop中的 dispatchMessage()方法入手,看看谁是该方法的调用者,深入Message源码中看看target的具体类型:

public final class Message implements Parcelable {

//代码省略

/package/ int flags;

/package/ long when;

/package/ Bundle data;

/package/ Handler target;

/package/ Runnable callback;

/package/ Message next;

//代码省略

}

从源码中我们可以看到其实target就是Handler类型。所以Handler是将消息发送到消息队列暂时存储下,然后又将消息发送给Handler自身去处理。那我们继续到Handler源码中去看看Handler是如何处理消息的:

public void dispatchMessage(Message msg) {

if (msg.callback != null) {

handleCallback(msg);

} else {

if (mCallback != null) {

if (mCallback.handleMessage(msg)) {

return;

}

}

handleMessage(msg);

}

}

private static void handleCallback(Message message) {

message.callback.run();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值