记录下一下Launcher是啥时候启动,至于启动中所有步骤这里不细写。
记录于此,主要是方便自己的。
PS : 分析源码 Android 9.0(P)
正文
正常情况下,Launcher是Android中第一个启动的应用。Launcher是Android系统的桌面,也是提供进入其他应用的启动程序。
Launcher很重要,因此今天记录一下Launcher的启动过程。
进入正题。
SystemServer.java
frameworks\base\services\java\com\android\server\SystemServer.javaSystemServer是Zygote启动时fork出来的,而SystemServer中主要的工作是:
初始化环境,比如时间,时区,语言等准备主线程Looper加载libandroid_servers.so库初始化系统Context创建SystemServiceManager启动服务(引导服务,核心服务,其他服务)进入Looper循环
之前大概介绍过(《》),如果需要的可以回归看看。(其实我也忘了,,尴尬)
今天介绍的Launcher启动是在启动服务(引导服务,核心服务,其他服务)中的[其他服务]中启动的。
startOtherServices()
差不多最后,有调用mActivityManagerService.systemReady()
//略[内容很多省略]mActivityManagerService.systemReady(() -> {//略 mSystemServiceManager.startBootPhase( SystemService.PHASE_ACTIVITY_MANAGER_READY);//略 });ActivityManagerService.java
frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.javapublicvoidsystemReady(finalRunnablegoingCallback, TimingsTraceLogtraceLog) { synchronized(this) {//mSystemReady = false, if (mSystemReady) {//略 }//略[初始化] mSystemReady=true; }//略 finalPowerManagerInternalpmi=LocalServices.getService(PowerManagerInternal.class);//pmi不为null if (pmi!=null) { pmi.registerLowPowerModeObserver(ServiceType.FORCE_BACKGROUND_CHECK, state->updateForceBackgroundCheck(state.batterySaverEnabled)); updateForceBackgroundCheck( pmi.getLowPowerState(ServiceType.FORCE_BACKGROUND_CHECK).batterySaverEnabled); }//略 synchronized (this) {//为true if (!android.os.Bolt.BOLT_PERSIST_APP) { startPersistentApps(PackageManager.MATCH_DIRECT_BOOT_AWARE); } mBooting=true;//略//BOLT_EARLY_HOME为false,进入 if (!android.os.Bolt.BOLT_EARLY_HOME) { //进入这里 startHomeActivityLocked(currentUserId, "systemReady"); }//略 }}startHomeActivityLocked
booleanstartHomeActivityLocked(intuserId, Stringreason) { //略[下面部分也省略] //这里没有获取到Launcher的启动包名 Intentintent=getHomeIntent(); //根据getHomeIntent()中的intent进行获取Launcher包名信息 ActivityInfoaInfo=resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);//aInfo不为null if (aInfo!=null) { //设置启动包名和启动Activity intent.setComponent(newComponentName(aInfo.applicationInfo.packageName, aInfo.name)); aInfo=newActivityInfo(aInfo); aInfo.applicationInfo=getAppInfoForUser(aInfo.applicationInfo, userId); ProcessRecordapp=getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid, true); //app == null if (app==null||app.instr==null) { intent.setFlags(intent.getFlags() |FLAG_ACTIVITY_NEW_TASK);//正式启动Launcher的Activity mActivityStartController.startHomeActivity(intent, aInfo, myReason); } } returntrue;}getHomeIntent()
IntentgetHomeIntent() { Intentintent=newIntent(mTopAction, mTopData!=null?Uri.parse(mTopData) : null); //mTopComponent为null intent.setComponent(mTopComponent); intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);//添加CATEGORY_HOME属性 if (mFactoryTest!=FactoryTest.FACTORY_TEST_LOW_LEVEL) { intent.addCategory(Intent.CATEGORY_HOME); } returnintent;}这里获取的intent并没有Launcher的启动包名信息,而是:
Intent { act=android.intent.action.MAINcat=[android.intent.category.HOME] flg=0x100 }resolveActivityInfo()
private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { ActivityInfo ai = null; ComponentName comp = intent.getComponent(); try { //comp为null,因为之前没有获取到 if (comp为null != null) { // Factory test. ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); } else { //进入这里,根据[android.intent.category.HOME]进行获取 ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( intent, intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId); if (info != null) { ai = info.activityInfo; } } //ai= ActivityInfo{4bb04fb com.biumall.launcher.MainActivity} } catch (RemoteException e) { } return ai;}startHomeActivity
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) { mSupervisor.moveHomeStackTaskToTop(reason); //build模式 //obtainStarter()返回ActivityStarter //最后执行的ActivityStarter..execute() mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason) .setOutActivity(tmpOutRecord) .setCallingUid(0) .setActivityInfo(aInfo) .execute(); mLastHomeActivityStartRecord = tmpOutRecord[0]; //inResumeTopActivity=false if (mSupervisor.inResumeTopActivity) { mSupervisor.scheduleResumeTopActivities(); }}obtainStarter
//关注点是返回ActivityStarterActivityStarter obtainStarter(Intent intent, String reason) { return mFactory.obtain().setIntent(intent).setReason(reason);}这里返回ActivityStarter,跟Launcher启动Activity的一样流程。
ActivityStarter.java
frameworks\base\services\core\java\com\android\server\am\ActivityStarter.java
这里的mRequest.mayWait为false,也就是走else
int execute() { try { //如果有调用.setMayWait(userId),这里会设置过true if (mRequest.mayWait) { return startActivityMayWait(mRequest.caller, mRequest.callingUid, mRequest.callingPackage, mRequest.intent, mRequest.resolvedType, mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo, mRequest.resultWho, mRequest.requestCode, mRequest.startFlags, mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig, mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId, mRequest.inTask, mRequest.reason, mRequest.allowPendingRemoteAnimationRegistryLookup); } else { return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent, mRequest.resolvedType, mRequest.activityInfo, mRequest.resolveInfo, mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo, mRequest.resultWho, mRequest.requestCode, mRequest.callingPid, mRequest.callingUid, mRequest.callingPackage, mRequest.realCallingPid, mRequest.realCallingUid, mRequest.startFlags, mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.componentSpecified, mRequest.outActivity, mRequest.inTask, mRequest.reason, mRequest.allowPendingRemoteAnimationRegistryLookup); } } finally { onExecutionComplete(); }}这里不继续跟了,就是startActivity的启动过程。后续会单独分析Activity的启动。


