Handler
Handler:負(fù)責(zé)發(fā)送和接收消息
1财岔、創(chuàng)建Handler時調(diào)用的它的構(gòu)造函數(shù)Handler(null,false),主要獲取當(dāng)前線程的Looper對象和消息循環(huán)地啰。
public Handler(Callback callback, boolean async) {
mLooper = Looper.myLooper();
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
2、發(fā)送消息sendMessage
- sendMessage方法遣臼,發(fā)送消息最終調(diào)用的是Handler中的enqueueMessage方法彩届,MessageQueue中的enqueueMessage方法,在消息隊列中通過msg.when將消息放到合適的位置蹦渣。
private boolean enqueueMessage(MessageQueue queue, Message msg,
long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
```
* obtainMessage方法哄芜,它是從消息池里面取出一個消息,沒有時才創(chuàng)建消息效率高柬唯。
* postDelayed方法认臊,此方法需要傳一個Runnable對象,最終賦值給Message的callback屬性权逗。
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
######3美尸、分發(fā)消息dispatchMessage
public void dispatchMessage(Message msg) {
//如果callback不為空,直接調(diào)用
if (msg.callback != null) {
message.callback.run();
handleCallback(msg);
} else {
//創(chuàng)建Handler時斟薇,傳遞的Callback接口,默認(rèn)為null恕酸。
if (mCallback != null) {
if (mCallback.handleMesscage(msg)) {
return;
}
}
handleMessage(msg);//回調(diào)子類方法堪滨,在Handler類里面只是聲明。
}
}
###Looper:
* 1蕊温、ThreadLocal類
它的作用是為每一個線程保存一份變量袱箱,內(nèi)部其實用一個數(shù)組來表示遏乔,對索引進(jìn)行散列保證內(nèi)部均勻。
>ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
* 2发笔、prepare方法準(zhǔn)備操作
準(zhǔn)備Looper類盟萨,在Looper構(gòu)造函數(shù)里面創(chuàng)建消息隊列和獲取當(dāng)前線程。將線程和Looper進(jìn)行關(guān)聯(lián)起來了讨。
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created
per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
* 3捻激、loop方法
使消息進(jìn)行分發(fā)
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;
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
return;// No message indicates that the message
}
msg.target.dispatchMessage(msg);//消息分發(fā)
msg.recycle();//回收消息
}
}
###Message:
* 屬性target
發(fā)送消息時將消息的target屬性設(shè)置為this(就是Handler),在通過msg.target.dispatchMessage方法可以找到發(fā)送消息的Handler前计。
###MessageQueue:
* 1胞谭、獲取消息
queue.next(),通過msg.when進(jìn)行計算出合適的Message.
###ActivityThread
Android程序入口函數(shù)是ActivityThread的main函數(shù),在這里先調(diào)用Looper.prepareMainLooper方法男杈,創(chuàng)建ActivityThread.
final ApplicationThread mAppThread = new ApplicationThread();
private void attach(boolean system) {
if (!system) {
RuntimeInit.setApplicationObject(mAppThread.asBinder());
final IActivityManager mgr = ActivityManagerNative.getDefault();
mgr.attachApplication(mAppThread);
} else {
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
mInstrumentation = new Instrumentation();
ContextImpl context = ContextImpl.createAppContext(this,
getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
}
}
public static void main(String[] args) {
//創(chuàng)建
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
//
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
Looper.loop();
}