IdleHandler只在第一次循环没有取到消息时,执行(如果有IdleHandler需要执行)
注释:
// We only ever reach this code block during the first iteration.
第一次循环的末尾执行nextPollTimeoutMillis = 0
所以第二次的循环,nextPollTimeoutMillis = 0
之后的循环nextPollTimeoutMillis根据是否还有消息分为,-1和
(int) Math.min(msg.when - now, Integer.MAX_VALUE)
-1是没有消息,另外一个是还不到第一条消息的执行时间
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
final long ptr = mPtr;
if (ptr == 0) {
return null;
}
int pendingIdleHandlerCount = -1; // -1 only during first iteration
int nextPollTimeoutMillis = 0;
for (;;) {
if (nextPollTimeoutMillis != 0) {
Binder.flushPendingCommands();
}
nativePollOnce(ptr, nextPollTimeoutMillis);
synchronized (this) {
// Try to retrieve the next message. Return if found.
final long now = SystemClock.uptimeMillis();
Message prevMsg = null;
Message msg = mMessages;
if (msg != null && msg.target == null) {
// Stalled by a barrier. Find the next asynchronous message in the queue.
do {
prevMsg = msg;
msg = msg.next;
} while (msg != null && !msg.isAsynchronous());
}
if (msg != null) {
if (now < msg.when) {
// Next message is not ready. Set a timeout to wake up when it is ready.
nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
} else {
// Got a message.
mBlocked = false;
if (prevMsg != null) {
prevMsg.next = msg.next;
} else {
mMessages = msg.next;
}
msg.next = null;
if (DEBUG) Log.v(TAG, "Returning message: " + msg);
msg.markInUse();
return msg;
}
} else {
// No more messages.
nextPollTimeoutMillis = -1;
}
// Process the quit message now that all pending messages have been handled.
if (mQuitting) {
dispose();
return null;
}
// If first time idle, then get the number of idlers to run.
// Idle handles only run if the queue is empty or if the first message
// in the queue (possibly a barrier) is due to be handled in the future.
if (pendingIdleHandlerCount < 0
&& (mMessages == null || now < mMessages.when)) {
pendingIdleHandlerCount = mIdleHandlers.size();
}
if (pendingIdleHandlerCount <= 0) {
// No idle handlers to run. Loop and wait some more.
mBlocked = true;
continue;
}
if (mPendingIdleHandlers == null) {
mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
}
mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
}
// Run the idle handlers.
// We only ever reach this code block during the first iteration.
for (int i = 0; i < pendingIdleHandlerCount; i++) {
final IdleHandler idler = mPendingIdleHandlers[i];
mPendingIdleHandlers[i] = null; // release the reference to the handler
boolean keep = false;
try {
keep = idler.queueIdle();
} catch (Throwable t) {
Log.wtf(TAG, "IdleHandler threw exception", t);
}
if (!keep) {
synchronized (this) {
mIdleHandlers.remove(idler);
}
}
}
// Reset the idle handler count to 0 so we do not run them again.
pendingIdleHandlerCount = 0;
// While calling an idle handler, a new message could have been delivered
// so go back and look again for a pending message without waiting.
nextPollTimeoutMillis = 0;
}
}
private static void handleCallback(Message message) {
message.callback.run();
}
分发消息流程:
当 Message 的 msg.callback 不为空时,则回调方法 msg.callback.run();当 Handler 的 mCallback 不为空时,则回调方法 mCallback.handleMessage(msg);最后调用 Handler 自身的回调方法 handleMessage(),该方法默认为空, Handler子类通过覆写该方法来完成具体的逻辑。
消息分发的优先级:
Message 的回调方法: message.callback.run(),优先级最高;Handler 中 Callback 的回调方法: Handler.mCallback.handleMessage(msg),优先级仅次于 1;Handler 的默认方法: Handler.handleMessage(msg),优先级最低。对于很多情况下,消息分发后的处理方法是第 3 种情况,即Handler.handleMessage(),一般地往往通过覆写该方法从而实现自己的业务逻
辑。