标签:
MessageQueue代码:http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/os/MessageQueue.java/?v=source
Handler代码:
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/os/Handler.java/?v=source
Looper代码:
http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/os/Looper.java/?v=source
handler的消息机制主要牵扯三方面:
消息发送:
public final boolean More ...sendMessage(Message msg) { return sendMessageDelayed(msg, 0); }
往下走:
public final boolean More ...sendMessageDelayed(Message msg, long delayMillis) { if (delayMillis < 0) { delayMillis = 0; } return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); }
最后走到这里:
public boolean More ...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); }
调用 enqueueMessage 在消息队列中插入一条消息,在 enqueueMessage总中,会把 msg.target 设置为当前的Handler 对象。
private boolean More ...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("MessageQueue", 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; }
每个线程只能运行一个looper对象,创建 Looper 的时候,内部会创建一个消息队列MessageQueue;并且looper在线程里面必须先prepare(),再loop来对消息队列messageQueue来进行遍历循环操作。
public static void loop() { final Looper me = myLooper(); //如果looper对象是空的,就提示必须在当前线程通过looper.prepare()来进行创建 if (me == null) { throw new RuntimeException("No Looper; Looper.prepare() wasn‘t called on this thread."); } //如果looper不为null,就取出消息队列 final MessageQueue queue = me.mQueue; 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; } //----------------队列不为null的操作---------------- // 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); } //----------------队列不为null,分发数据出去---------------- 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(); } }
循环遍历并分发消息出去msg.target.dispatchMessage(msg):
/** * Handle system messages here. *首先,如果消息的 callback 不是空,便调用 handleCallback 处理。否则判断 Handler 的 mCallback 是否为空,不为空则调用它的 handleMessage方法。如果仍然为空,才调用 Handler 自身的 handleMessage,也就是我们创建 Handler 时重写的方法 */ public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } //回调handler的handleMessage(msg)方法,从而接收数据 handleMessage(msg); } }
额,先写到这,明天抽时间再看下,尽可能减少错误。
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/5460251.html