Android Handler消息处理机制详解

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/u011692041/article/details/51371367

前言

从我们学习android开始,几乎每天都在和handler打交道.有了它,我们在子线程中处理好了耗时的操作,可以利用它来更新UI.它为我们在线程间的通信提供了很大的方便,而今天博客就来详细的介绍一下Handler的消息循环机制,一步一步的了解其中的奥妙,本文不介绍Handler的详细使用,探究的是内部的原理.所以看这篇博客的童鞋需要有handler的基本使用能力
本博客基于Android 6.0源码讲解

先抛出一个简单的使用例子

public class DemoAct extends AppCompatActivity {

    private Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(DemoAct.this, "收到啦", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_demo);
    }

    /**
     * 按钮的点击事件
     *
     * @param view
     */
    public void clickView(View view) {
        new Thread(){
            @Override
            public void run() {
                h.sendEmptyMessage(0);
            }
        }.start();
    }

}


效果图:

从图中我们可以看到一个简单的使用,那么里面的实现原理到底是怎么样的呢?
下面我们一起来探究!

上图以在主线程中做了一个图解,画出了大致的流程
图片画的是不是很棒?大笑以后请叫我神笔马良~~~(脸皮厚模式)

MessageQueue和Looper的介绍

在Android中,一个线程可以对应一个Looper对象,不能有多个,为什么说可以呢,Looper作为一个消息的循环器,在一个线程中可以使用它也可以不使用它,所以一个线程中可以有一个Looper对象不能有多个.
说到了消息的循环器,就必须掰扯掰扯所谓的消息队列MessageQueue.每一个Looper对象里面都会维护一个消息队列MessageQueue,它是由Message对象构成的一个链表的结构,按照时间的先后顺序排列,Message的next属性关联下一个Message对象.

Handler的介绍

Handler如何发送消息

Handler是我们发送消息和处理消息的一个类,那么在上述的图解中,到底是如何实现发送消息的呢?

    public final boolean sendEmptyMessage(int what)
    {
        return sendEmptyMessageDelayed(what, 0);
    }
上面是发送一空消息的源码,可以看到调用了另一个方法,那么点进去~~~
    public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageDelayed(msg, delayMillis);
    }
在这里我们可以明确的看到,在我们发送消息的时候,如何发送的不是一个Message对象,而是一个空消息,那么它也会自动为我们创建一个Message对象,说到底其实最后发送出去的肯定是一个Message对象
继续点进去
    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);
    }

在这个方法中对一个成员变量进行了为空的判断,但是并不抛出,而是将异常打印一下,那么这个成员变量又是什么呢?
其实他就是这个handler所在线程的Looper中的消息队列!咦,你之前不是说消息队列在Looper里面么?怎么在Handler里面也有一份啊?
瞧瞧handler的构造函数便可知道!
    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());
            }
        }

        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

从构造函数中我们可以看到,从Looper里面拿到当前线程的Looper对象,然后从里面拿出来消息队列,所以这里就解释了上面那个成员变量的问题
那么继续点之前的方法
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

最关键的把消息放入队列的代码来啦

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

其中最最关键的代码

    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;

从这里可以看到,我们的消息是按照时间的先后排列的,而这个所谓的消息的集合是一个类似链表的结构.
使用Message对象的next属性进行连接起来的

到这里位置,消息总算被送进了消息队列中,上面讲述了Handler是如何发送消息的

Handler是如何处理消息的

我们在上面的流程图中可以看出,取出消息是谁干的?是Looper,没错就是它,所以我们就去看Looper的代码
Looper.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();

        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
            Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            msg.target.dispatchMessage(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();
        }
    }

MessageQueue 作为消息队列,通过next方法拿到一个消息对象,知道存储消息是按照时间排序的,所以拿的过程就是拿第一个消息而已,因为之前存的时候已经是按照时间排序的。代码在MessageQueue类中next()的方法中,这里就不贴出了.

从这个方法的注释来看,我们就可以知道这个方法的功能:
也就是此方法开启了一个死循环来拿出每一个消息,拿出来之后,通过消息对象中的target对象(其实就是之前发送消息的Handler的一个引用)的dispatchMessage(Message msg)方法来分发(处理)消息,贴出分发(处理)的代码:
    /**
     * Handle system messages here.
     */
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

代码很简单,判断了两个callback(后面解释),都为null的话就调用handleMessage(msg),此方法就是我们使用handler的时候最常用的方法了,上面的示例代码中就是重写了这个方法,可以回头看看~~~

handler中的callback是什么?

在Handler源码中有如下一个接口:
    /**
     * Callback interface you can use when instantiating a Handler to avoid
     * having to implement your own subclass of Handler.
     *
     * @param msg A {@link android.os.Message Message} object
     * @return True if no further handling is desired
     */
    public interface Callback {
        public boolean handleMessage(Message msg);
    }
在Message对象中有这么一段:

所以上面提到的在分发(处理)消息的时候的mCallback其实就是一个接口,接口的方法也是handleMessage.
那还有一个是Message对象中的callback,这个是一个Runnable接口
请注意:这个虽然是Runnable接口,但是别看这个是线程中经常用到的接口,你就认为这里面可以处理耗时的操作,这里是不允许的,否则会阻塞主线程
所以你创建Handler的时候就可以为所欲为了:

1.使用Handler中的Callback接口

    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            
            return false;
        }
    });
这里的返回值是控制消息是否继续传递给handler中的handleMessage方法执行
    public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }
从消息的分发(处理)这里可以看到,如果返回了true,那么handler中的handleMessge方法是不会被执行的

2.使用Message对象中的callback

Message m = Message.obtain(h, new Runnable() {
        @Override
        public void run() {
                //做一些事情,这个run方法是主线程调用的
        }
});
h.sendMessage(m);


这就是Handler处理消息的过程,你是否对Handler的发送和处理消息有一定的理解了呢?

一些大家应该比较想问的问题

消息循环器,我并没有开启,也就是并没有在主线程中调用Looper.loop,为什么主线程中的消息循环器就有作用呢?

答案在这:
    public static void main(String[] args) {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
        SamplingProfilerIntegration.start();

        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);

        Environment.initForCurrentUser();

        // Set the reporter for event logging in libcore
        EventLogger.setReporter(new EventLoggingReporter());

        AndroidKeyStoreProvider.install();

        // Make sure TrustedCertificateStore looks in the right place for CA certificates
        final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
        TrustedCertificateStore.setDefaultUserDirectory(configDir);

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();

        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }

        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

这是ActivityThread类中的main方法,可以看到里面帮我们初始化了Looper
对应的代码是:Looper.prepareMainLooper()
开启了消息循环器
对应的代码是:Looper.loop();

创建多个Handler来处理消息,为什么可以区分开,而不干扰

在发送消息的时候,有这么一段:
    private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }
可以很清楚的看到,消息对象Message对象的target就是这个发送消息的handler,所以处理消息的时候就是根据这个target找到原来的那个handler,然后交由它来处理

Looper中的loop()方法是死循环,为什么没有卡死


我用红框框框住的就是从消息队列中获取下一个消息,后面有一句注释,说这个方法可能会阻塞,所以在没有消息的时候是会阻塞的
有的人可能还会有疑问:
那我没有发送消息,一个app中都没有发送任何handler消息,那为啥app还是正常走,也没有卡死
答:你没有发送消息,不代表系统中没有发送消息,在Android中使用了大量的Handler消息机制

猜你喜欢

转载自blog.csdn.net/u011692041/article/details/51371367