深入探索Android消息机制之Handler

之前写了一篇关于Message的文章,感兴趣的朋友可以去看一下【Android消息机制之Message解析(面试)】,这一次咱们来聊一下消息机制中用得最多的Handler,也是面试中问得最多的之一,在这里我先抛几个问题出来:

1.Handler、Looper、Thread有什么关系?
2.为什么在子线程创建handler会抛异常 "Can't create handler inside thread that has not called Looper.prepare()"?
3.如何使用handler来处理message?
4.为什么不能在子线程更新UI?

我们带着问题去看源码:

大家先对一下的对象,脑补一下工厂的情景:

Handler:消息的处理者,工厂中流水线的工人。
Message:系统传递的消息,工厂中流水线上的产品。
MessageQueue:消息队列,工厂中流水线上的传送带。
Looper:发动机,工厂中使流水线的传送带运动的发动机。

我们先从程序入口来进行分析,Android应用程序的入口在ActivityThread的main函数中,我们先从main函数进行分析:

ActivityThread.java:

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

    // 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>");

    //在android应用程序的入口其实在ActivityThread的main方法
    //在这里,主线程会创建一个Looper对象。
    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");
}

代码很长,我们挑关键的代码来看,在代码中我已写上注释,在main函数中,Looper调用了prepareMainLooer(),我们再进去Looper看看。

Looper.java:

/**
 * Initialize the current thread as a looper, marking it as an
 * application's main looper. The main looper for your application
 * is created by the Android environment, so you should never need
 * to call this function yourself.  See also: {@link #prepare()}
 */
public static void prepareMainLooper() {
    //在主线程中,其默认初始化一个Looper对象,因此我们在主线程的操作中是不需要自己去调prepare()。
    prepare(false);
    synchronized (Looper.class) {
        //这里先进行判断,在主线程是否已经存在Looper了,
        // 避免我们手动去调用prepareMainLooper(),因为这个是给程序入口初始化的时候系统会自动调用的
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        //设置全局变量,主线程的looper
        sMainLooper = myLooper();
    }
}

Initialize the current thread as a looper, marking it as an* application's main looper. The main looper for your application* is created by the Android environment, so you should never need* to call this function yourself.

注意这个函数的注释,大概意思是:在主线程创建一个looper,是这个主线程的主looper,当这个app在初始化的时候就会自行创建,因此这个函数不是给你们调用的,是给系统自身在程序创建的时候调用的。

我们继续往下看,有个prepare(boolean)函数,我们去看看这个到底是用来干什么的。
Looper.java:

private static void prepare(boolean quitAllowed) {
    //先判断当前线程是否已经存在Looper了,如果存在,不允许设置新的Looper对象,一个线程只允许存在一个Looper
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    //在当前线程中,创建新的Looper对象,并绑定当前线程
    sThreadLocal.set(new Looper(quitAllowed));
}

在这里,我们看到了sThreadLocal,我们先看看这个sThreadLocal在Looper是干什么用的。

Looper.java:

// sThreadLocal.get() will return null unless you've called prepare().
//sThreadLocal在Looper中作为全局变量,用于保存每个线程中的数据,可以看做是容器
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

Looper中,sThreadLocal作为一个全局变量,sThreadLocal其实是保存Looper的一个容器,我们继续往ThreadLocal的get、set进行分析。

ThreadLocal.java:

/**
 * Returns the value in the current thread's copy of this
 * thread-local variable.  If the variable has no value for the
 * current thread, it is first initialized to the value returned
 * by an invocation of the {@link #initialValue} method.
 *
 * @return the current thread's value of this thread-local
 */
public T get() {
    //获取当前线程保存的对象--通过get函数来获取Looper对象
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null)
            return (T) e.value;
    }
    return setInitialValue();
}

/**
 * Sets the current thread's copy of this thread-local variable
 * to the specified value.  Most subclasses will have no need to
 * override this method, relying solely on the {@link #initialValue}
 * method to set the values of thread-locals.
 *
 * @param value the value to be stored in the current thread's copy of
 *              this thread-local.
 */
public void set(T value) {
    //把当前的looper保存到当前线程中
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null)
        map.set(this, value);
    else
        createMap(t, value);
}

我们看到关键的代码:
Thread t=Thread.currentThread();

也就是说,我们的Looper对象分别保存在相对应的线程中。我们看回来我们的prepare(boolean)函数:
looper.java:

private static void prepare(boolean quitAllowed) {
    //先判断当前线程是否已经存在Looper了,如果存在,不允许设置新的Looper对象,一个线程只允许存在一个Looper
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    //在当前线程中,创建新的Looper对象,并绑定当前线程
    sThreadLocal.set(new Looper(quitAllowed));
}

Looper.prepare(boolean)的作用就是创建一个Looper对象,并与当前线程绑定在一起。在代码中,首先判断当前线程是否已经存在looper,如果不存在则创建新的looper并且绑定到当前的线程上。

再看回之前的代码:
looper.java:

**
 * Initialize the current thread as a looper, marking it as an
 * application's main looper. The main looper for your application
 * is created by the Android environment, so you should never need
 * to call this function yourself.  See also: {@link #prepare()}
 */
public static void prepareMainLooper() {
    //在主线程中,其默认初始化一个Looper对象,因此我们在主线程的操作中是不需要自己去调prepare()。
    prepare(false);
    synchronized (Looper.class) {
        //这里先进行判断,在主线程是否已经存在Looper了,
        // 避免我们手动去调用prepareMainLooper(),因为这个是给程序入口初始化的时候系统会自动调用的
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        //设置全局变量,主线程的looper
        sMainLooper = myLooper();
    }
}

分别看一下sMainLooper是什么,myLooper()又是什么?
Looper.java:

//保存一个主线程的looper
private static Looper sMainLooper;  // guarded by Looper.class

/**
 * Return the Looper object associated with the current thread.  Returns
 * null if the calling thread is not associated with a Looper.
 */
public static Looper myLooper() {
    //使用当前线程的looper
    return sThreadLocal.get();
}

sMainLooper在Looper做为一个全局变量,保存主线程绑定的looper,myLooper()则是获取当前线程绑定的Looper。在prepareMainLooper()中,在主线程中创建一个新的Looper,并且绑定主线程中,同时把这个主线程的looper赋值给sMainLooer这个全局变量。

ActivityThread.java:

   public static void main(String[] args) {
        ......

        //在android应用程序的入口其实在ActivityThread的main方法
        //在这里,主线程会创建一个Looper对象。
        Looper.prepareMainLooper();

        ......
        ......
        ......        
        //执行消息循环
        Looper.loop();

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

在应用程序ActivityThread.main入口中,系统除了调用Looper.prepareMainLooper,而且在最后还调用了Looper.loop(),这个函数有什么?大家脑补一下,工厂里的流水线上,除了有传送带外,如果你不让它动起来,那传送带也没什么作用,那么Looper.loop的作用就是让这个传送带动起来,也就是我们的让我们的消息队列动起来。

Looper.java:

/**
 * 调用此函数用于启动消息队列循环起来,作用相当于工厂流水线中的传送带的开关,
 * 只有把开关打开,传送带才跑起来
 * Run the message queue in this thread. Be sure to call
 * {@link #quit()} to end the loop.
 */
public static void loop() {
    //先进行判断当前线程是否有绑定looper
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    //获取这个looper的消息队列
    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
        final Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        final long traceTag = me.mTraceTag;
        if (traceTag != 0) {
            Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
        }
        try {
            //关键点,这里的msg.target也就是hanlder.看回代码hanlder.enqueueMessage()
            msg.target.dispatchMessage(msg);
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }

        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);
        }
        //最后回收这个message
        msg.recycleUnchecked();
    }
}

这一段代码比较长,我们挑有中文注释的来看,先判断当前的线程是否存在looper,如果存在获取保存在Looper的消息队列messagequeue,然后无限循环这个消息队列来获取message,注意我们留到了一段代码:

//关键点,这里的msg.target也就是hanlder.看回代码hanlder.enqueueMessage()
msg.target.dispatchMessage(msg);

msg.target其实就是我们的handler,无论是handler通过post或者sendEmptyMessage,最终都会调用到调到这个enqueueMessage(),在这里会将handler赋值到msg.target中.

Handler.java:

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    //在message中放一个标记
    msg.target = this;
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    //在这里把消息放到队列里面去
    return queue.enqueueMessage(msg, uptimeMillis);
}

既然Looper中的loop()调用了msg.target.dispatchMessage,我们就看看Handler的dispatchMessage是如何进行处理这个msg的。

Handler.java:

/**
 * Handle system messages here.
 */
public void dispatchMessage(Message msg) {
    //这里先判断callback是否为空
    // callback就是我们使用handler.post(Runnable r)的入参runnable
    if (msg.callback != null) {
        handleCallback(msg);
    } else {
        //如果hanlder的入参callback不为空,优先处理
        if (mCallback != null) {
            //如果回调返回true.则拦截了handler.handleMessage的方法
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        //这就是为什么我们使用hanlder的时候,需要重写handleMessage的方法
        handleMessage(msg);
    }
}

在dispatchMessage函数中,意思就是分发这个消息,在代码中先判断msg.callback是否为空,msg.callback是什么?在上一篇文章已详细介绍过了,其实就是handler.post中的runnable对象,通俗的来说就是handler如果有post操作的,就处理post的操作,我们在看看handlerCallback这个函数。
Handler.java:

private static void handleCallback(Message message) {
    message.callback.run();
}

很简单,就一行代码,我们看到了熟悉的run方法,这个不就是我们使用post的时候传进去的Runnbale对象的run方法吗?

/**
 * 模拟开始
 */
private void doSth() {
    //开启个线程,处理复杂的业务业务
    new Thread(new Runnable() {
        @Override
        public void run() {
            //模拟很复杂的业务,需要1000ms进行操作的业务
            ......
            handler.post(new Runnable() {
                @Override
                public void run() {
                    //在这里可以更新ui
                    mTv.setText("在这个点我更新了:" + System.currentTimeMillis());
                }
            });
        }
    }).start();
}

我们回到handler.dispatchMessage(Message)中,如果不是通过post那么callback就为空,我们看到了一个mCallback变量,我们看看这个Callback的定义:

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

/**
 * Constructor associates this handler with the {@link Looper} for the
 * current thread and takes a callback interface in which you can handle
 * messages.
 * <p>
 * If this thread does not have a looper, this handler won't be able to receive messages
 * so an exception is thrown.
 *
 * @param callback The callback interface in which to handle messages, or null.
 */
public Handler(Callback callback) {
    this(callback, false);
}

The callback interface in which to handle messages, or null.
我们可以通过实现这个接口,并作为一个参数传进去Handler来达到处理这个消息的效果。

Handler.java:

/**
 * Handle system messages here.
 */
public void dispatchMessage(Message msg) {
    //这里先判断callback是否为空
    // callback就是我们使用handler.post(Runnable r)的入参runnable
    if (msg.callback != null) {
        handleCallback(msg);
    } else {
        //如果hanlder的入参callback不为空,优先处理
        if (mCallback != null) {
            //如果回调返回true.则拦截了handler.handleMessage的方法
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        //这就是为什么我们使用hanlder的时候,需要重写handleMessage的方法
        handleMessage(msg);
    }
}

最后一行代码中,我们看到了熟悉的handleMessage,这不就是我们经常handler.handlerMessage的方法吗?
Demo:

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        //处理消息
    }
};

但注意之前我们所看到的,如果我们mCallback.handlerMessage(msg)返回为true的话,这样就不交给handler.handleMessage处理了。

我们继续看回来我们的Looper.loop()
Looper.java:

/**
 * 调用此函数用于启动消息队列循环起来,作用相当于工厂流水线中的传送带的开关,
 * 只有把开关打开,传送带才跑起来
 * Run the message queue in this thread. Be sure to call
 * {@link #quit()} to end the loop.
 */
public static void loop() {
    .....
    .....
    //循环通过消息队列来获取消息
    for (; ; ) {
        ......
        //最后回收这个message
        msg.recycleUnchecked();
    }
}

在无限循环每个消息的时候,除了调用handler.dispatchMessage,最后还会调用msg.recycleUnchecked()进行回收这个消息,至于message怎么回收我们就不讨论了,详情的大家可以去看看我上一篇文章。

现在我们回过头看看我之前说的那2个问题:为什么子线程创建handler会抛异常?
我们先看看Handler的关键的构造器:

/**
 * Default constructor associates this handler with the {@link Looper} for the
 * current thread.
 * <p>
 * If this thread does not have a looper, this handler won't be able to receive messages
 * so an exception is thrown.
 */
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());
        }
    }
//获取当前线程的looper
     mLooper = Looper.myLooper();
//如果当前线程没有绑定looper则抛异常
    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;
}

看构造器可得,如果在子线程创建handler,必须在子线程中先创建一个Looper对象,不然hanlder在初始化的时候获取不了当前线程的looper,会抛出异常"Can't create handler inside thread that has not called Looper.prepare()"。

因此,如果我们在子线程中创建Handler的时候,我们可以这样:

new Thread(new Runnable() {
    @Override
    public void run() {
        //在当前子线程创建一个Looper对象
        Looper.prepare();
        Handler handler=new Handler();
        //让消息队列动起来
        Looper.loop();
    }
}).start();

这样就能成功在子线程创建handler。

总结:

1.为什么在主线程中创建Handler不需要我们调用Looper.prepare().因为在程序的入口中系统会调用Looper.prepareMainLooper()来创建,并且让其主线程的Looper启动起来。如果我们在子线程创建handler,需要手动创建looper并且启动。

2.每一个线程只能存在一个Looper, Looper有一个全局变量sThreadLocal用来保存每一个线程的looper,通过get、set进行存取looper。

3.Handler可以通过通过post或者sendMessage进行发送消息,因为其最终会调用sendMessageDelayed,我们可以通过runnable方式或者重写handleMessage进行消息的处理,当然如果通过handler.sendMessage(msg)的方式的话,我们可以实现Callback接口达到消息的处理。

4.为什么不能在子线程更新UI?其实更准确的来说应该是UI只能在创建UI的线程中进行更新,也就是主线程,如果子线程创建UI,其可以在子线程进行更新。

注:
网上也有很多文章关于Android消息机制的文章,大神们都写得很透彻,但为什么我还要去写这样的文章,一方便想通过读源码来提高个人的学习能力,另一方面也想将自己学到的分享给大家,哪怕多年以后看回自己的博客,也是别有一番滋味。从14年开始出来做android,看了不少文章,学到了很多东西,我很敬佩他们。特别感谢郭霖、李纪钢、徐宜生等等,因为他们的无私分享让很多人在工作上解决了不少难题,因此我也希望我能成为这样的人。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容