新聞中心
應(yīng)用程序的主線程準(zhǔn)備就好消息隊(duì)列并且進(jìn)入到消息循環(huán)后,其它地方就可以往這個(gè)消息隊(duì)列中發(fā)送消息了。

我們繼續(xù)以文章開始介紹的Android應(yīng)用程序啟動(dòng)過(guò)程源代碼分析一文中的應(yīng)用程序啟動(dòng)過(guò)為例,說(shuō)明應(yīng)用程序是如何把消息加入到應(yīng)用程序的消息隊(duì)列中去的。
在Android應(yīng)用程序啟動(dòng)過(guò)程源代碼分析這篇文章的Step 30中,ActivityManagerService通過(guò)調(diào)用ApplicationThread類的scheduleLaunchActivity函 數(shù)通知應(yīng)用程序。
它可以加載應(yīng)用程序的默認(rèn)Activity了,這個(gè)函數(shù)定義在frameworks/base/core/java/android /app/ActivityThread.java文件中:
- [java] view plaincopypublic final class ActivityThread {
- ......
- private final class ApplicationThread extends ApplicationThreadNative {
- ......
- // we use token to identify this activity without having to send the
- // activity itself back to the activity manager. (matters more with
- ipc)
- public final void scheduleLaunchActivity(Intent intent, IBinder token, int
- ident,
- ActivityInfo info, Bundle state, List pendingResults,
- List pendingNewIntents, boolean notResumed, boolean isForward)
- {
- ActivityClientRecord r = new ActivityClientRecord();
- r.token = token;
- r.ident = ident;
- r.intent = intent;
- r.activityInfo = info;
- r.state = state;
- r.pendingResults = pendingResults;
- r.pendingIntents = pendingNewIntents;
- r.startsNotResumed = notResumed;
- r.isForward = isForward;
- queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
- }
- ......
- }
- ......
- }
這里把相關(guān)的參數(shù)都封裝成一個(gè)ActivityClientRecord對(duì)象r,然后調(diào)用queueOrSendMessage函數(shù)來(lái)往應(yīng)用程序的消息隊(duì) 列中加入一個(gè)新的消息(H.LAUNCH_ACTIVITY),這個(gè)函數(shù)定義在frameworks/base/core/java/android /app/ActivityThread.java文件中:
- [java] view plaincopypublic final class ActivityThread {
- ......
- private final class ApplicationThread extends ApplicationThreadNative {
- ......
- // if the thread hasn't started yet, we don't have the handler, so just
- // save the messages until we're ready.
- private final void queueOrSendMessage(int what, Object obj) {
- queueOrSendMessage(what, obj, 0, 0);
- }
- ......
- private final void queueOrSendMessage(int what, Object obj, int arg1, int
- g2) {
- synchronized (this) {
- ......
- Message msg = Message.obtain();
- msg.what = what;
- msg.obj = obj;
- msg.arg1 = arg1;
- msg.arg2 = arg2;
- mH.sendMessage(msg);
- }
- }
- ......
- }
- ......
- }
網(wǎng)站標(biāo)題:Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(10)
標(biāo)題鏈接:http://www.fisionsoft.com.cn/article/cccgego.html


咨詢
建站咨詢
