Lanucher就是我们平时的开机后的桌面APP(它其实就是一个APP,没什么特别的,看起来有点高大上),作为APP程序,它也需要启动自己的Activity界面(因为桌面是能够看得见的,有界面的).那么开机时如何启动呢?
根据上一篇,开机时SystemServer会将ActivityManagerService启动,那么首先就会启动SystemReady(...)方法.
public void systemReady(final Runnable goingCallback) {
里面的程序非常的多,大概判断了FactoryTest,Binder,SystemUI一些,还有一些记录信息,但是只需要看到最后面:
mMainStack.resumeTopActivityLocked(null);
看过前面一篇的,对应上面这句话不会陌生的.接下来转到ActivityStack类中:不过记住上面传递的参数为null
final boolean resumeTopActivityLocked(ActivityRecord prev) { return resumeTopActivityLocked(prev, null); } final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) { // Find the first activity that is not finishing. ActivityRecord next = topRunningActivityLocked(null); // Remember how we'll process this pause/resume situation, and ensure // that the state is reset however we wind up proceeding. final boolean userLeaving = mUserLeaving; mUserLeaving = false; if (next == null) { // There are no more activities! Let's just start up the // Launcher... if (mMainStack) { ActivityOptions.abort(options); return mService.startHomeActivityLocked(mCurrentUser); } }
前面传递的参数为null,发现参数是ActivityRecord,那么就是很容易,现在系统里面还没有任何运行过的Activity记录,当然显而易见,刚开机怎么可能有运行Activity记录呢?
那么下面的程序next==null自然而然就是成立的,然后又要装到ActivityManagerService类:
boolean startHomeActivityLocked(int userId) { if (mHeadless) { // Added because none of the other calls to ensureBootCompleted seem to fire // when running headless. ensureBootCompleted(); return false; } if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL && mTopAction == null) { // We are running in factory test mode, but unable to find // the factory test app, so just sit around displaying the // error message and don't try to start anything. return false; } 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); } ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); 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); if (app == null || app.instrumentationClass == null) { intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); mMainStack.startActivityLocked(null, intent, null, aInfo, null, null, 0, 0, 0, 0, null, false, null); } } return true; }
最后的mMainStack.startActivityLocked(...)就会启动Launcher APP,这个在上面的程序中打印出log信息,后面的startActivityLocked和前一篇的是一样的了.
private final void startActivityLocked(ActivityRecord r, boolean newTask, boolean doResume, boolean keepCurTransition, Bundle options) { final int NH = mHistory.size(); int addPos = -1; if (!newTask) { // If starting in an existing task, find where that is... boolean startIt = true; for (int i = NH-1; i >= 0; i--) { ActivityRecord p = mHistory.get(i); if (p.finishing) { continue; }
打印log出上面NH的值会是等于0,因为Launcher是第一个启动的Activity.
这里面特别强调的是mHistory是ArrayList<ActivityRecord>,记录了启机以后所有Activity的运行状态信息,每启动一个Activity就会对应有一个ActivityRecord将其所有的状态和信息记录下来,并且将这个new的ActivityRecord添加到mHistory集合中.启动机器后,mHistory中肯定是空的,所以NH=0.