Android Handler解析

在ActivityThread中

  1. 第一步 调用 Looper类中的
    Looper.prepareMainLooper();
方法 给ActivityThread绑定一个Looper对象(ActivityThread只有一个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();
    }
}

进入prepare方法 把Looper对象存入ThreadLocal之中 (单例)

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

然后执行myLooper方法从ThreadLocal取出Looper对象

public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

准备阶段完毕
2.调用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;//取出Looper中的消息队列

      //循环取出消息队列中的消息  并发送给Handler 
    for (;;) {
        Message msg = queue.next(); // might block
        //...
        if (msg == null) {
            // No message indicates that the message queue is quitting.
            return;
        }

        //...
        msg.target.dispatchMessage(msg);

        //....

        msg.recycleUnchecked();
    }
}

接下来把Message 传入进入Handler类中的dispatchMessage方法

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

一般写Callback接口回调,所以执行Callback接口中handleMessage方法 此方法体为空,具体怎么处理Message要自己实现
public void handleMessage(Message msg) {
}
此方法就是我们Activity中Handler对象中的方法如下:

Handler mHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
       //这里进行UI更新等操作
        return false;
    }
})

;

3.怎么把消息发送到MessageQueue中
调用Handler的sendMessage()等方法

1.
public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }
2.
 public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }
3.
 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);
    }

把消息加入MessageQueue

 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

这样MessageQueue中有Message了,在ActivityThread的死循环中取出消息并处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值