1.handler构造过程
public Handler() {
this(null, false);
}
该构造函数最终会调用
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());
}
}
mLooper = Looper.myLooper();
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;
}
这分别为 成员变量 mLooper = Looper.myLooper();和 mQueue = mLooper.mQueue; 赋值
2.mLooper 获取过程
通过 Looper.myLooper(); 得到
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
get()方法实现 看注释
public T get() {
// Optimized for the fast path.
Thread currentThread = Thread.currentThread();
// 获取当前线程的values
Values values = values(currentThread);
if (values != null) {
// 得到threadLocal数组
Object[] table = values.table;
int index = hash & values.mask;
if (this.reference == table[index]) {// 弱引用与数组中的threadLocal是同个引用
return (T) table[index + 1];
}
} else {
// 初始化values
values = initializeValues(currentThread);
}
// 得到value
return (T) values.getAfterMiss(this);
}
3.mQueue 获取过程
mQueue = mLooper.mQueue;
mQueue在Looper的私有构造函数中初始化
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
私有构造函数在prepare(boolean quitAllowed) 调用
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
// 创建出looper
sThreadLocal.set(new Looper(quitAllowed));
}
repare(boolean quitAllowed) 方法 在 prepareMainLooper()中调用
public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
prepareMainLooper()方法 在ActivityThread 的main中调用
// ....
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");
消息队列初始化完成
在main方法中调用Looper.loop();
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;
// 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);
}
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();
}
}
这注意for循环 Message msg = queue.next(); 是一个阻塞方法
msg.target.dispatchMessage(msg); 这句是处理系统消息
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
回调子类实现的方法mCallback.handleMessage(msg)