Android4.4 Framework分析——Android預設Home應用Launcher3的載入過程分析

來源:互聯網
上載者:User

標籤:launcher3啟動過程

本文主要介紹Android4.4預設Home應用Launcher3的啟動過程和Launcher3的資料載入過程。Launcher的啟動是開機時,ActivityManagerService準備好後開始的,是它的啟動順序圖表:


step1,SystemServer中,ActivityManagerService準備好了。

step3,

    boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,            Bundle targetOptions) {        if (targetStack == null) {            targetStack = getFocusedStack(); //獲得mHomeStack        }        boolean result = false;        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {            final ActivityStack stack = mStacks.get(stackNdx);            if (isFrontStack(stack)) {                if (stack == targetStack) {                    result = stack.resumeTopActivityLocked(target, targetOptions);                } else {                    stack.resumeTopActivityLocked(null);                }            }        }        return result;    }

mStacks中已經添加過mHomeStack,mStacks的內容介紹請查看 Android4.4 Framework分析——ActivityManagerService的啟動和對Activity的管理,目前mStacks中只有mHomeStack這個ActivityStacK。

step6,

   boolean startHomeActivityLocked(int userId) {        if (mHeadless) {            // Added because none of the other calls to ensureBootCompleted seem to fire            // when running headless.            ensureBootCompleted();            return false;        }        Intent intent = getHomeIntent(); //HOME intent        ActivityInfo aInfo =            resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);//尋找home應用資訊        if (aInfo != null) {            intent.setComponent(new ComponentName(                    aInfo.applicationInfo.packageName, aInfo.name));            // Don't do this if the home app is currently being            // instrumented.            aInfo = new ActivityInfo(aInfo);            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);            ProcessRecord app = getProcessRecordLocked(aInfo.processName,                    aInfo.applicationInfo.uid, true);//home應用未啟動,返回null            if (app == null || app.instrumentationClass == null) {                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);                mStackSupervisor.startHomeActivity(intent, aInfo); //step7            }        }        return true;    }

    Intent getHomeIntent() {        Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);        intent.setComponent(mTopComponent);        if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {            intent.addCategory(Intent.CATEGORY_HOME);//Home應用的標誌        }        return intent;    }

step7~step10,Activity的啟動過程參照Android4.4 Framework分析——Launcher中啟動應用程式(startActivity)的過程

的step14之後的過程。

step11~step14,開始非同步載入應用表徵圖,背景工作執行緒sWorkerThread。

step15~step20,載入workspace的表徵圖,step16讀取LauncherProvider的Favorites資料,favorites表的資料是Launcher資料庫建立時從default_workspace.xml解析讀取的。step18綁定表徵圖資訊。

step22,睡眠等待空閑進程,然後載入主菜單的所有app表徵圖。

step23,從睡眠中醒來,準備載入所有app表徵圖,包括widget等。

step24,

        private void loadAllApps() {            final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;            .....            final PackageManager packageManager = mContext.getPackageManager();            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); //在PackageManagerService查詢所有要顯示在案頭上的app            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);            // Clear the list of apps            mBgAllAppsList.clear();            // Query for the set of apps            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);            if (DEBUG_LOADERS) {                Log.d(TAG, "queryIntentActivities took "                        + (SystemClock.uptimeMillis()-qiaTime) + "ms");                Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");            }            // Fail if we don't have any apps            if (apps == null || apps.isEmpty()) {                return;            }            // Sort the applications by name            final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;            Collections.sort(apps,                    new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));//按應用程式名稱排序            // Create the ApplicationInfos            for (int i = 0; i < apps.size(); i++) {                ResolveInfo app = apps.get(i);                // This builds the icon bitmaps.                mBgAllAppsList.add(new AppInfo(packageManager, app,                        mIconCache, mLabelCache));            }            // Huh? Shouldn't this be inside the Runnable below?            final ArrayList<AppInfo> added = mBgAllAppsList.added;            mBgAllAppsList.added = new ArrayList<AppInfo>();            // Post callback on main thread            mHandler.post(new Runnable() {                public void run() {                    final long bindTime = SystemClock.uptimeMillis();                    final Callbacks callbacks = tryGetCallbacks(oldCallbacks);                    if (callbacks != null) {                        callbacks.bindAllApplications(added); //step26,綁定                        if (DEBUG_LOADERS) {                            Log.d(TAG, "bound " + added.size() + " apps in "                                + (SystemClock.uptimeMillis() - bindTime) + "ms");                        }                    } else {                        Log.i(TAG, "not binding apps: no Launcher activity");                    }                }            });        }



右鍵複製圖片地址,在瀏覽器中開啟即可查看大圖。

未完待續,有不對的地方,請指正。


Android4.4 Framework分析——Android預設Home應用Launcher3的載入過程分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.