Handler 线程间通信的一种机制,同时也可以传送数据。
Looper 轮询机制,不断获取队列里的消息,让handler目标去处理消息
MsgQueue 消息队列,保存msg消息的。
msg 数据的载体。
handler.sendMessage(msg)
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}
默认发送延迟为0的事件
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
// SystemClock.uptimeMillis() 自启动后的毫秒数
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) {
//发送的目标对象 就是当前的handler对象
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
// 队列处理消息msg
return queue.enqueueMessage(msg, uptimeMillis);
}
进入MessageQueue 对象,找到方法enqueueMessage
//when 什么时间发送,默认当前立即发送
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) { //目标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;
//队列里的msg对象
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;
// 无限循环,把msg 放进队列里,这个队列是个链表结构的
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对象主要是轮询消息,轮询就是获取队列的每个消息,然后交给Handler目标对象分发消息。
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;
...省略
boolean slowDeliveryDetected = false;
// 无限循环 获取队列里的msg,然后由目标对象分发消息,target==Handler对象
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);
}
....
try {
// 目标对象分发消息,去接收处理
msg.target.dispatchMessage(msg);
dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
。。。。。。。。。省略
}
}
/**
* Handle system messages here.
* callback 不为null 就执行callback的run方法
*
*/
public void dispatchMessage(Message msg) {
// callback 不为null 就执行callback的run方法
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg); //分发消息,也就是我们最终去重写的方法
}
}
/** 分发消息,也就是我们最终去重写的方法
* Subclasses must implement this to receive messages.
*/
public void handleMessage(Message msg) {
}