1.概述

在Android中使用消息机制,最常见的就是Handler,主要作用用来实现不同线程之间的通信。通常情况下,因为 Android 不能在主线程执行耗时操作,Handler的主要使用场景就是在子线程执行耗时操作后将消息发送到主线程,从而进行UI的更新。

2. Handler 实例

先看一个使用Handler的简单实例:先不考虑Handler造成的内存泄漏问题

package cn.zzw.messenger.handlerdemo;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.lang.ref.WeakReference;public class MainActivity extends AppCompatActivity {    private MyHandler mHandler;    private TextView mTv;    private Button mBtn;    private class MyHandler extends Handler {        @Override        public void handleMessage(@NonNull Message msg) {            if (msg.what == 10086) {                mTv.setText(String.valueOf(msg.arg1));            }        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mHandler = new MyHandler();        mTv = findViewById(R.id.mTv);        mBtn = findViewById(R.id.mBtn);        mBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                new Thread() {                    @Override                    public void run() {                        try {                            int i = 0;                            while (i < 100) {                                i++;                                Thread.sleep(300);                                Message msg = Message.obtain();                                msg.what = 10086;                                msg.arg1 = i;                                mHandler.sendMessage(msg);                            }                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }.start();            }        });    }}

步骤:

a. 创建 Handler 对象

b.创建Message对象,封装消息

c.调用 mHandler.sendMessage(msg) 从子线程发送消息

d.在Handler中重写 handleMessage(@NonNull Message msg) 方法,接收消息并处理消息。

 

3. Handler消息机制源码解析

初始化Handler对象的时候,使用的是无参的构造方法:

    public Handler() {        this(null, false);    }

可以看到它内部调用的是另外一个两个参数的构造方法:

    public Handler(Callback callback, boolean async) {        if (FIND_POTENTIAL_LEAKS) {            final Class<? extends Handler> klass = getClass();            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&                    (klass.getModifiers() & Modifier.STATIC) == 0) {                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +                    klass.getCanonicalName());            }        }        //1        mLooper = Looper.myLooper();        if (mLooper == null) {            throw new RuntimeException(                "Can't create handler inside thread " + Thread.currentThread()                        + " that has not called Looper.prepare()");        }        //2        mQueue = mLooper.mQueue;        mCallback = callback;        mAsynchronous = async;    }

 这个构造方法主要作用是获取 mLooper 和 mQueue 对象,其中 mLooper 是通过 Looper.myLooper() 获取。

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

 在myLooper 方法中,Looper对象是从 sThreadLocal中获取。

创建 ThreadLocal 对象:

 static final ThreadLocal sThreadLocal = new ThreadLocal();

ThreadLocal 是确保当前线程有且只有一个Looper对象。 做了软件开发几年了,只有在这个Looper的源码中才接触到ThreadLocal,上次在正航软件面试的时候,聊到 ThreadLocal ,只答出了 ThreadLocal 的作用,并不了 ThreadLocal 的原理,后续会研究下 ThreadLocal 的原理,并写一篇相关的文章。

扯远了,继续拉回正题。

看下 Looper 对象是如何被放到 ThreadLocal 中的:

    public static void prepare() {        prepare(true);    }    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));    }

prepare() 是一个私有的静态的方法。如果 ThreadLocal 中已经存在Looper对象,这时候会抛出异常。只有没有Looper对象的时候,会创建一个新的 Looper 对象。

看看 Looper 的构造方法:

    private Looper(boolean quitAllowed) {        mQueue = new MessageQueue(quitAllowed);        mThread = Thread.currentThread();    }

在这个构造方法中:

 创建了 MessageQueue 对象,所以在 Handler 的构造方法中才能从Looper中获取到 MessageQueue。

而这个 Handler 中 用到的 Looper 对象是什么时候创建的呢?

在主线程的 ActivityThread 的main 方法中创建的:

在主线程中调用了 :

Looper.prepareMainLooper();

Looper.loop();

具体代码:

prepareMainLooper 方法:

    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对象。 

通过上面的分析,Handler,Looper,MessageQueue 都有。而他们是怎么进行消息的传送。

消息传送首先要发送消息,调用的是 Handler 的 sendMessage() 方法。

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

上面代码的流程是:sendMessage() → sendMessageDelayed() → sendMessageAtTime() → enqueueMessage()

在 sendMessageAtTime() 方法中,拿到 构造方法中获取的 mQueue。将消息(msg)和 消息队列(mQueue) 传入方法enqueueMessage() 中。

再看看 enqueueMessage() :

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

 在Handler类的 enqueueMessage() 方法中,调用 queue.enqueueMessage() 将消息放入消息队列中。

MessageQueue 类中的 enqueueMessage() : 在此方法中,进行JNI调用,最终调用到Native 方法:nativeWake。Native方法就不往下继续追了。

    boolean enqueueMessage(Message msg, long when) {        if (msg.target == null) {            throw new IllegalArgumentException("Message must have a target.");        }        if (msg.isInUse()) {            throw new IllegalStateException(msg + " This message is already in use.");        }        synchronized (this) {            if (mQuitting) {                IllegalStateException e = new IllegalStateException(                        msg.target + " sending message to a Handler on a dead thread");                Log.w(TAG, e.getMessage(), e);                msg.recycle();                return false;            }            msg.markInUse();            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;                needWake = mBlocked;            } 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.                needWake = mBlocked && p.target == null && msg.isAsynchronous();                Message prev;                for (;;) {                    prev = p;                    p = p.next;                    if (p == null || when < p.when) {                        break;                    }                    if (needWake && p.isAsynchronous()) {                        needWake = false;                    }                }                msg.next = p; // invariant: p == prev.next                prev.next = msg;            }            // We can assume mPtr != 0 because mQuitting is false.            if (needWake) {                nativeWake(mPtr);            }        }        return true;    }

以上步骤 Handler 已经将消息发送到消息队列中了。

这时候在回头看在 ActivityThread 的main 方法中,调用到了 Looper 的 loop() 方法。

loop 方法 :

    /**     * Run the message queue in this thread. Be sure to call     * {@link #quit()} to end the 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;        // Make sure the identity of this thread is that of the local process,        // and keep track of what that identity token actually is.        Binder.clearCallingIdentity();        final long ident = Binder.clearCallingIdentity();        // Allow overriding a threshold with a system prop. e.g.        // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'        final int thresholdOverride =                SystemProperties.getInt("log.looper."                        + Process.myUid() + "."                        + Thread.currentThread().getName()                        + ".slow", 0);        boolean slowDeliveryDetected = false;        for (;;) {            Message msg = queue.next(); // might block            if (msg == null) {                // No message indicates that the message queue is quitting.                return;            }            // This must be in a local variable, in case a UI event sets the logger            final Printer logging = me.mLogging;            if (logging != null) {                logging.println(">>>>> Dispatching to " + msg.target + " " +                        msg.callback + ": " + msg.what);            }            final long traceTag = me.mTraceTag;            long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;            long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;            if (thresholdOverride > 0) {                slowDispatchThresholdMs = thresholdOverride;                slowDeliveryThresholdMs = thresholdOverride;            }            final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);            final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);            final boolean needStartTime = logSlowDelivery || logSlowDispatch;            final boolean needEndTime = logSlowDispatch;            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));            }            final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;            final long dispatchEnd;            try {                msg.target.dispatchMessage(msg);                dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;            } finally {                if (traceTag != 0) {                    Trace.traceEnd(traceTag);                }            }            if (logSlowDelivery) {                if (slowDeliveryDetected) {                    if ((dispatchStart - msg.when) <= 10) {                        Slog.w(TAG, "Drained");                        slowDeliveryDetected = false;                    }                } else {                    if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",                            msg)) {                        // Once we write a slow delivery log, suppress until the queue drains.                        slowDeliveryDetected = true;                    }                }            }            if (logSlowDispatch) {                showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);            }            if (logging != null) {                logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);            }            // Make sure that during the course of dispatching the            // identity of the thread wasn't corrupted.            final long newIdent = Binder.clearCallingIdentity();            if (ident != newIdent) {                Log.wtf(TAG, "Thread identity changed from 0x"                        + Long.toHexString(ident) + " to 0x"                        + Long.toHexString(newIdent) + " while dispatching to "                        + msg.target.getClass().getName() + " "                        + msg.callback + " what=" + msg.what);            }            msg.recycleUnchecked();        }    }

在这个方法中,开启一个死循环,不断的调用:Message msg = queue.next();

MessageQueue 类中的 next() 方法:

    Message next() {        // Return here if the message loop has already quit and been disposed.        // This can happen if the application tries to restart a looper after quit        // which is not supported.        final long ptr = mPtr;        if (ptr == 0) {            return null;        }        int pendingIdleHandlerCount = -1; // -1 only during first iteration        int nextPollTimeoutMillis = 0;        for (;;) {            if (nextPollTimeoutMillis != 0) {                Binder.flushPendingCommands();            }            nativePollOnce(ptr, nextPollTimeoutMillis);            synchronized (this) {                // Try to retrieve the next message.  Return if found.                final long now = SystemClock.uptimeMillis();                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 && !msg.isAsynchronous());                }                if (msg != null) {                    if (now < msg.when) {                        // Next message is not ready.  Set a timeout to wake up when it is ready.                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);                    } else {                        // Got a message.                        mBlocked = false;                        if (prevMsg != null) {                            prevMsg.next = msg.next;                        } else {                            mMessages = msg.next;                        }                        msg.next = null;                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);                        msg.markInUse();                        return msg;                    }                } else {                    // No more messages.                    nextPollTimeoutMillis = -1;                }                // Process the quit message now that all pending messages have been handled.                if (mQuitting) {                    dispose();                    return null;                }                // If first time idle, then get the number of idlers to run.                // Idle handles only run if the queue is empty or if the first message                // in the queue (possibly a barrier) is due to be handled in the future.                if (pendingIdleHandlerCount < 0                        && (mMessages == null || now < mMessages.when)) {                    pendingIdleHandlerCount = mIdleHandlers.size();                }                if (pendingIdleHandlerCount <= 0) {                    // No idle handlers to run.  Loop and wait some more.                    mBlocked = true;                    continue;                }                if (mPendingIdleHandlers == null) {                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];                }                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);            }            // Run the idle handlers.            // We only ever reach this code block during the first iteration.            for (int i = 0; i < pendingIdleHandlerCount; i++) {                final IdleHandler idler = mPendingIdleHandlers[i];                mPendingIdleHandlers[i] = null; // release the reference to the handler                boolean keep = false;                try {                    keep = idler.queueIdle();                } catch (Throwable t) {                    Log.wtf(TAG, "IdleHandler threw exception", t);                }                if (!keep) {                    synchronized (this) {                        mIdleHandlers.remove(idler);                    }                }            }            // Reset the idle handler count to 0 so we do not run them again.            pendingIdleHandlerCount = 0;            // While calling an idle handler, a new message could have been delivered            // so go back and look again for a pending message without waiting.            nextPollTimeoutMillis = 0;        }    }

这样消息就再次被拿出来了,并调用了:msg.target.dispatchMessage(msg); 而这个 target 实际是一个 Handler 对象。

Handler 类 中的 dispatchMessage()方法:

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

在这个方法中看到了非常熟悉的 handleMessage() 方法,在重写的 handleMessage 方法就可以执行我们期望做的事情了。

 

4.总结

在子线程执行完耗时操作,当Handler发送消息时,将会调用MessageQueue.enqueueMessage,向消息队列中添加消息。当通过Looper.loop开启循环后,会不断地从线程池中读取消息,即调用MessageQueue.next,然后调用目标Handler(即发送该消息的Handler)的dispatchMessage方法传递消息,然后返回到Handler所在线程,目标Handler收到消息,调用handleMessage方法,接收消息,处理消息。

可以用下图来理解:

参考:

https://github.com/LRH1993/android_interview/blob/master/android/basis/message-mechanism.md

https://www.jianshu.com/p/91a4b797553d

更多相关文章

  1. C#/IOS/Android通用加密解密方法
  2. Android动画效果-更新中
  3. Android(安卓)AIDL 双向调用的使用及相关原理
  4. Android监听事件四种方法
  5. Android中的图形图像
  6. Android(安卓)AIDL(Binder) 原理分析
  7. android camera
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. 分析脚本文件AndroidInitProcess分析心得
  2. 【转】Android背景选择器Selector详解
  3. [置顶] Android学好Shape不再依赖美工
  4. Android 基础知识
  5. Android从Linux系统启动
  6. 在ListItem某组件添加响应事件
  7. 「Android Tips」解决 Mac OSX 无法识别
  8. Android系统信息获取 之十:移动网络相关信
  9. Android 使用grade实现Android 项目debug
  10. Android Geocoder(位置解析)