美文网首页待写
Android 手写 Handler 源码

Android 手写 Handler 源码

作者: 是刘航啊 | 来源:发表于2021-01-12 17:20 被阅读0次
Handler 流程中比较重要的类
  1. ActivityThread
  2. Looper
  3. MessageQueue
  4. Message
  5. Activity
  6. View

首先实例化一个 Message,通过 handler.sendMessage 发送消息,然后在 handler.handleMessage 中更新 UI

Message
public class Message {
    public Object obj;
    public Handler target;
    public long when;
    public Message next;
}
Handler
public class Handler {
    
    MessageQueue mQueue;

    public Handler() {
        Looper looper = Looper.myLooper();
        if (looper == null) {
            throw new RuntimeException(
                    "Can't create handler inside thread " + Thread.currentThread()
                            + " that has not called Looper.prepare()");
        }
        mQueue = looper.mQueue;
    }  
    
    public void handleMessage(Message msg) {

    }

    public void sendMessage(Message msg) {
        sendMessageDelayed(msg, 0);
    }

    public final boolean sendMessageDelayed(Message msg, long delayMillis) {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, System.currentTimeMillis() + delayMillis);
    }

    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        return enqueueMessage(queue, msg, uptimeMillis);
    }

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

Handler.sendMessage 实际上是执行 MessageQueue.enqueueMessage 方法,将 Message 加入队列

MessageQueue -> enqueueMessage
public class MessageQueue {
    private Message mMessages;

    boolean enqueueMessage(Message msg, long when) {
        synchronized (this) {
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                Message prev;
                for (; ; ) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }
        }
        return true;
    }

    public Message next() {
        for (; ; ) {
            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = System.currentTimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg.target == null) {
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null);
                }
                if (msg != null) {
                    if (now < msg.when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                    } else {
                        // Got a message.
                        if (prevMsg != null) {
                            prevMsg.next = msg.next;
                        } else {
                            mMessages = msg.next;
                        }
                        msg.next = null;
                        return msg;
                    }
                } else {
                    // No more messages.
                }
            }
        }
    }
}
ActivityThread
public class ActivityThread {
    final H mH = new H();

    /**
     * 主入口
     * @param args
     */
    public static void main(String[] args) {
        Looper.prepare();
        ActivityThread thread = new ActivityThread();
        thread.attach(false);
        Looper.loop();
    }

    private void attach(boolean b) {
        Activity testActivity = new TestActivity();
        testActivity.onCreate();

        Message message = new Message();
        message.obj = testActivity;
        //通过 Handler 执行生命周期
        mH.sendMessage(message);
    }

    class H extends Handler {

        public void handleMessage(Message msg) {
            Activity activity = (Activity) msg.obj;
            activity.onResume();
        }

    }
}

ActivityThread 会执行 Looper.prepare() 和 Looper.loop() 方法,同时通过 H 类唤起 Activity

Looper
public class Looper {
    static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    public MessageQueue mQueue;

    public Looper(){
        mQueue = new MessageQueue();
    }

    public static void prepare() {
        sThreadLocal.set(new Looper());
    }

    public static void loop() {
        Looper looper = myLooper();
        for (;;) {
            MessageQueue queue = looper.mQueue;
            Message message = queue.next();
            if (message == null) {
                return;
            }
            message.target.handleMessage(message);
        }
    }

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

Looper 类中会有 set 和 get 方法。set 方法是设置一个 (mainThread,Looper) 的键值对。get 方法时通过当前 Thread 获取对应的 Looper。
Looper.loop() 方法会循环便利 MessageQueue,然后通过 Handler 处理消息。

主要类基本上介绍完了,如果有什么不懂的地方可以看源码。

Github 源码链接

如果有什么写得不对的,可以在下方评论留言,我会第一时间改正。

相关文章

网友评论

    本文标题:Android 手写 Handler 源码

    本文链接:https://www.haomeiwen.com/subject/ayfeaktx.html