上周对Android中的事件派发机制进行了分析,此次博主要对消息队列和Looper的源码进行简单的分析。你们耐心看下去,其实消息队列的逻辑比事件派发机制简单多了,因此你们确定会很容易看懂的。java
消息队列在android中对应MessageQueue这个类,顾名思义,消息队列中存放了大量的消息(Message)android
消息(Message)表明一个行为(what)或者一串动做(Runnable),有两处会用到Message:Handler和Messenger多线程
Handler你们都知道,主要用来在线程中发消息通知ui线程更新ui。Messenger能够翻译为信使,能够实现进程间通讯(IPC),Messenger采用一个单线程来处理全部的消息,并且进程间的通讯都是经过发消息来完成的,感受不能像AIDL那样直接调用对方的接口方法(具体有待考证),这是其和AIDL的主要区别,也就是说Messenger没法处理多线程,全部的调用都是在一个线程中串行执行的。Messenger的典型代码是这样的:new Messenger(service).send(msg),它的本质仍是调用了Handler的sendMessage方法async
Looper是循环的意思,它负责从消息队列中循环的取出消息而后把消息交给目标处理ide
线程若是没有Looper,就没有消息队列,就没法处理消息,线程内部就没法使用Handler。这就是为何在子线程内部建立Handler会报错:"Can't create handler inside thread that has not called Looper.prepare()",具体缘由下面再分析。oop
在线程的run方法中加入以下两句:源码分析
Looper.prepare();
post
Looper.loop();
ui
这一切不用咱们来作,有现成的,HandlerThread就是带有Looper的线程。this
想用线程的Looper来建立Handler,很简单,Handler handler = new Handler(thread.getLooper()),有了上面这几步,你就能够在子线程中建立Handler了,好吧,其实android早就为咱们想到这一点了,也不用本身写,IntentService把咱们该作的都作了,咱们只要用就行了,具体怎么用后面再说。
一个Handler会有一个Looper,一个Looper会有一个消息队列,Looper的做用就是循环的遍历消息队列,若是有新消息,就把新消息交给它的目标处理。每当咱们用Handler来发送消息,消息就会被放入消息队列中,而后Looper就会取出消息发送给它的目标target。通常状况,一个消息的target是发送这个消息的Handler,这么一来,Looper就会把消息交给Handler处理,这个时候Handler的dispatchMessage方法就会被调用,通常状况最终会调用Handler的handleMessage来处理消息,用handleMessage来处理消息是咱们经常使用的方式。
源码分析
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); }
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(); } }
/** * 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),相信你们都经常使用这个方法吧
/** * 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的子类可是还能照样处理消息,恐怕你们经常使用的方式都是新new一个Handler而后override其handleMessage方法来处理消息吧,从如今开始,咱们知道,不建立Handler的子类也能够处理消息。多说一句,为何建立Handler的子类很差?这是由于,类也是占空间的,一个应用class太多,其占用空间会变大,也就是应用会更耗内存。
@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()是死循环,你的code根本执行不了,不过貌似你能够把你的code放在super.run()以前执行,可是这好像不是主流玩法,因此不建议这么作。
public void onCreate() { // TODO: It would be nice to have an option to hold a partial wakelock // during processing, and to have a static startService(Context, Intent) // method that would launch the service & hand off a wakelock. super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }IntentService继承自Service,它是一个抽象类,其被建立的时候就new了一个HandlerThread和ServiceHandler,有了它,就能够利用IntentService作一些优先级较高的task,IntentService不会被系统轻易杀掉。使用IntentService也是很简单,首先startService(intent),而后IntentService会把你的intent封装成Message而后经过ServiceHandler进行发送,接着ServiceHandler会调用onHandleIntent(Intent intent)来处理这个Message,onHandleIntent(Intent intent)中的intent就是你startService(intent)中的intent,ok,如今你须要作的是从IntentService派生一个子类并重写onHandleIntent方法,而后你只要针对不一样的intent作不一样的事情便可,事情完成后IntentService会自动中止。因此,IntentService是除了Thread和AsyncTask外又一执行耗时操做的方式,并且其不容易被系统干掉,建议关键操做采用IntentService。
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。