Message是如何觸發的
還是ActivityThread這段代碼。來自Android中為什么主線程不會因為Looper.loop()里的死循環阻塞?
我們知道APP的入口是在ActivityThread,一個Java類,有著main方法,而且main方法中的代碼也不是很多.
public static void main(String[] args) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
SamplingProfilerIntegration.start();
// CloseGuard defaults to true and can be quite spammy. We
// disable it here, but selectively enable it later (via
// StrictMode) on debug builds, but using DropBox, not logs.
CloseGuard.setEnabled(false);
Environment.initForCurrentUser();
// Set the reporter for event logging in libcore
EventLogger.setReporter(new EventLoggingReporter());
AndroidKeyStoreProvider.install();
// Make sure TrustedCertificateStore looks in the right place for CA certificates
final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
TrustedCertificateStore.setDefaultUserDirectory(configDir);
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
// End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
在上篇文章MessageQueue與Looper的由來我們得知這段代碼中的Looper.prepareMainLooper()
在主線程中創建了一個Looper對象,然后在這段代碼的末尾處,調用了Looper.loop()
方法,我們來看看Looper.loop()
源碼:
//刪除部分代碼
public static void loop() {
//獲取當前線程的Looper對象
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;
...
for (;;) {
//不斷的從消息隊列拿出消息隊列的第一條消息,直到沒有消息為止
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
...
try {
msg.target.dispatchMessage(msg);
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
...
msg.recycleUnchecked();
}
}
從這個loop()
方法中邏輯就比較明了了,里面有一個死循環,從消息隊列中不斷的取出消息,然后調用這個方法msg.target.dispatchMessage(msg)
,msg.tagre
為當初你創建的Handler對象,因為在Handler把消息放入消息隊列的時候執行了以下代碼:
//Handler類
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
可以看到這里把創建的Handler賦值給了msg.target
。然后msg.target.dispatchMessage(msg)
就相當于handler.dispatchMessage(msg)
,所以我們來看看Handler的dispatchMessage()方法:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
//mCallback為當初創建Handler時傳入的Callback對象
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
private static void handleCallback(Message message) {
message.callback.run();
}
如果看過第一篇Handler消息發送我們就可以了解到Handler是如何發送消息的,所以看到這段代碼就應該知道這些消息或者是回調是如何發送的了。
根據自己的理解,繪制了以下流程圖,如有錯誤,請及時提醒我改正:這個系列是本人寫的第一篇比較完成的博客,肯定會有非常多的不足,希望大家能夠多留言批評,希望能明確指出文章中可能有的錯誤,我會及時更正,謝謝。
系列目錄: