1. 什么是消息队列
消息队列在Android中对应MessageQueue这个类,消息队列中存放了大量的消息(Message)
2.什么是消息
消息(Message)代表一个行为(what)或者一串动作(Runnable),有两处会用到Message:Handler和Messenger
3.什么是Handler
Handler主要用来在子线程更新UI和线程间通信
4.什么是Looper
Looper负责从消息队列中循环的取出消息然后把消息交给目标处理
5.如何让子线程有Looper从而正常使用Handler?
在线程的run方法中加入如下两句:
Looper.prepare();
Looper.loop();
HandlerThread就是带有Looper的线程。
想用线程的Looper来创建Handler,很简单,Handler handler = new Handler(thread.getLooper()),有了Looper,就可以在子线程中创建Handler了。
消息队列和Looper的工作机制
一个Handler会有一个Looper,一个Looper会有一个消息队列,Looper的作用就是循环的遍历消息队列,如果有新消息,就把新消息交给它的目标处理。每当我们用Handler来发送消息,消息就会被放入消息队列中,然后Looper就会取出消息发送给它的目标target。一般一个消息的target是发送这个消息的Handler,这样Looper就会把消息交给Handler处理,这个时候Handler的dispatchMessage方法就会被调用,一般情况最终会调用Handler的handleMessage来处理消息,用handleMessage来处理消息是我们常用的方式。
看源码:
1. Handler发送消息
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); } private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { msg.target = this; if (mAsynchronous) { msg.setAsynchronous(true); } //这里msg被加入消息队列queue return queue.enqueueMessage(msg, uptimeMillis); }
2.Looper的工作过程
public static void loop() { 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 Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } //将消息交给target处理,这个target就是Handler类型 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.recycle(); }
}
3.Handler如何处理消息
/** * Subclasses must implement this to receive messages. */ public void handleMessage(Message msg) { } /** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { //这个方法很简单,直接调用msg.callback.run(); handleCallback(msg); } else { //如果我们设置了callback会由callback来处理消息 if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } //否则消息就由这里来处理,这是我们最常用的处理方式 handleMessage(msg); } }
msg.callback和mCallback是啥东西?
/*package*/ Runnable callback;
现在已经很明确了,msg.callback是个Runnable,什么时候会设置这个callback:handler.post(runnable),接着往下看,一目了然,就是常用的post(Runnable 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); }
final Callback mCallback;
而mCallback是个接口,可以这样来设置 Handler handler = new Handler(callback),这个callback的意义是什么呢,代码里面的注释已经说了,可以让你不用创建Handler的子类但是还能照样处理消息,我们知道了,不创建Handler的子类也可以处理消息。
HandlerThread简介
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }
HandlerThread继承自Thread,其在run方法内部为自己创建了一个Looper,使用上HandlerThread和普通的Thread不一样,无法执行常见的后台操作,只能用来处理新消息,这是因为Looper.loop()是死循环。
在子线程创建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()); } } //获取当前线程的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; }
如何避免:在UI线程使用Handler或者给子线程加上Looper。