之前分析了SystemServer 启动 而Launcher 启动就是在之后进行的 位置就在 SystemServer 的run 方法内 开始
private void run() {
........
// Start services.
startBootstrapServices();
startCoreServices();
startOtherServices();
........
}
这里启动了几个服务 其中 我们看看 otherServices 都有什么 这里附上ActivityManagerService 的源码位置
private void startOtherServices() {
......
//调用了activitymanagerservice 的方法
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
}
......
}
//方法最后启动了launcher actiity
public void systemReady(final Runnable goingCallback) {
.................
synchronized (this) {
try {
List apps = AppGlobals.getPackageManager().
getPersistentApplications(STOCK_PM_FLAGS);
if (apps != null) {
int N = apps.size();
int i;
for (i=0; i<N; i++) {
ApplicationInfo info
= (ApplicationInfo)apps.get(i);
if (info != null &&
!info.packageName.equals("android")) {
addAppLocked(info, false, null /* ABI override */);
}
}
}
// Start up initial activity.
//开启launcheractivity
mBooting = true;
startHomeActivityLocked(mCurrentUserId, "systemReady");
}
可以看到最后是通过activitymanager service 启动了Launcher 然后我们看看startHomeActivityLocked 内部 是什么
boolean startHomeActivityLocked(int userId, String reason) {
.......
Intent intent = getHomeIntent();
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, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mStackSupervisor.startHomeActivity(intent, aInfo, reason);
}
}
return true;
}
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ?
Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
//public static final String CATEGORY_HOME =
"android.intent.category.HOME";
intent.addCategory(Intent.CATEGORY_HOME);//
}
return intent;
}
以上我们看到 是先创建了一个intent 用于启动launcher 然后设置了intent 的category 为Intent.CATEGORY_HOME
也就是 "android.intent.category.HOME" 然后我们找一下 http://androidxref.com/6.0.0_r1/xref/packages/apps/Launcher2/AndroidManifest.xml
这就是Launcher 的Manifest 文件
<application
android:name="com.android.launcher2.LauncherApplication"
android:label="@string/application_name"
android:icon="@mipmap/ic_launcher_home"
android:largeHeap="@bool/config_largeHeap"
android:supportsRtl="true">
<activity
android:name="com.android.launcher2.Launcher"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:resumeWhilePausing="true"
android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
//可以看到这个LauncherActiviry 的category 就是
android.intent.category.HOME
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
以上最后 就找到了LauncherActivity 然后 启动了桌面 我们就在开机以后看到了 桌面 而 应用图标等信息 是通过PackageManagerService 获取到的。这个过程本文没有写 有兴趣的可以自行研究