Android Handler解析

来源:互联网 发布:上知教育幼小衔接好吗 编辑:程序博客网 时间:2024/06/10 20:13

在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的死循环中取出消息并处理。

0 0
原创粉丝点击