看Activity启动流程中,记一下关于Launcher、SystemServer、Application的一些笔记。
Launcher
Launcher在onCreate时会创建LauncherModel并调用startLoader,创建LoaderTask。调用 loadWorkspace、bindWorkspace等。loadWorkspace会查询获取widget、shortcut、folder等一系列ItemInfo。对于ShortcutInfo,解析出包含intent,user等信息。这些ItemInfo将通过bindWorkspace传递给Launcher.bindItems,对于ShortcutInfo将创建BubbleTextView,并setTag将ShortcutInfo注入。于是乎,Launcher的onclick可以将view中的tag取出,若tag是ShortcutInfo类型,自然而然可以拿到用于启动activity的intent,和相关的user信息。
SystemServer
Activity启动流程中,LauncherAppsService.startActivityAsUser()使用了自己的Context:
public void startActivityAsUser(String callingPackage,
ComponentName component, Rect sourceBounds,
Bundle opts, UserHandle user) throws RemoteException {
...
for (int i = 0; i < size; ++i) {
ActivityInfo activityInfo = apps.get(i).activityInfo;
if (activityInfo.packageName.equals(component.getPackageName()) &&
activityInfo.name.equals(component.getClassName())) {
// Found an activity with category launcher that matches
// this component so ok to launch.
launchIntent.setComponent(component);
// 这里的context是SystemServer创建ContextImpl
mContext.startActivityAsUser(launchIntent, opts, user);
return;
}
}
...
}
那么context是从哪来的呢?我们知道LauncherAppsService是由SystemServer启动的:
// SystemServer
private void startOtherServices() {
...
mSystemServiceManager.startService(LauncherAppsService.class);
...
}
// SystemManagerService
public SystemService startService(String className) {
final Class<SystemService> serviceClass;
try {
serviceClass = (Class<SystemService>)Class.forName(className);
} catch (ClassNotFoundException ex) {
...
}
return startService(serviceClass);
}
public <T