1.ActivityManagerService
1.1 概述
ActivityManagerService负责系统中的四大组件的启动,切换,调度及应用进程的管理和调度等工作,其职责与操作系统中的检查能合格管理和调度模块类似。
1.2ActivityManagerService
private void run() {
...
//初始化得到一个context对象
createSystemContext();
// 创建system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// 启动service
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
...
}
ActivityManagerService的启动分散在startBootstrapServices();startCoreServices();startOtherServices()这三个启动函数之中,需要在这三个函数中去依次寻觅ActivityManagerService启动的踪迹。
不过在这之前有一个很重要的函数createSystemContext(),它创建了一个后续service启动所都需要的context对象,这是在android中很重要的一个类。通过它可以获取并操作Application对应的资源,类,甚至包含于Application中的四大组件。
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
//通过这个函数,会创建一个系统进程使用的system context
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar);
}
1.2.1 Context类介绍
Context对象是一个抽象类,ActivityManagerService创建的是它的子类ContextImpl。Activity类和Service类都是继承自Context类,他们都是Application的重要组成部分,那么一个APK中有多少个Context呢?(答案是,一个Activity就是一个Context,一个Service就是一个Context,因此有多少Activity和Service,就包含多少Context对象)
- ContextImpl类真正地实现了Context类的方法;
- ContextWrapper类是ContextImpl类的一个封装,里面通过mBase指向真正的方法实现,这样做是为了把ContextImpl类给隐藏起来
- ContextThemeWrapper类内部包含了与主题(Theme)相关的接口,这里所说的的主题是指在AndroidManifest.xml中通过android:theme为Application元素或者Activity元素指定的主题
- Application从ContextThemeWrapper派生,实现了ComponentCallbacks2接口。其有一个LoadedApk类型的成员变量mLoadedApk,代表一个APK。即一个Application对象会和一个APK绑定。
- Service和Activity类都从ContextThemeWrapper派生,都通过mApplication指向Application对象。
1.2.2 ActivityThread.systemMain函数
ActivityThread代表着应用程序的主线程,其职责就是调度及执行在该线程中运行的四大组件。而SystemServer是一个特殊的应用进程,在里面同时运行着framework-res.apk(里面包含着关机对话框等Activity)和SettingsProvider.apk。
public static ActivityThread systemMain() {
//判断是否需要硬件渲染加速
if (!ActivityManager.isHighEndGfx()) {
HardwareRenderer.disable(true);
} else {
HardwareRenderer.enableForegroundTrimming();
}
//创建一个ActivityThread对象
ActivityThread thread = new ActivityThread();
//attach()函数会创建一个context
thread.attach(true);
return thread;
}
(1)attach函数分析
这里的framework-res.apk这个package的相关信息是在getSystemContext()函数中通过新建LoadedApk类来实现的。getSystemContext()同样是一个单例模式,如果context对象不存在,则创建一个新的context对象。
private void attach(boolean system) {
sCurrentActivityThread = this;
mSystemThread = system;
//这里system为true
if (!system) {
......
} else {
设置DDMS时看到的systemserver进程名为system_process
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
//创建几个重要的类
mInstrumentation = new Instrumentation();
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
//创建一个Application对象
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
//调用Application类的onCreate()函数
mInitialApplication.onCreate();
}
......
}
// 与系统日志相关
DropBox.setReporter(new DropBoxReporter());
//注册Configuration发生变化的回调通知
ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
@Override
//在component正在运行时,如果系统配置发生变化时,需要调用该函数
public void onConfigurationChanged(Configuration newConfig) {
//mResourcesManager是在new ActivityThread对象时创建的应用程序资源类
synchronized (mResourcesManager) {
if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
......
}
}
}
public void onLowMemory() { }
public void onTrimMemory(int level) {}
});
这个函数创建了几个重要的类,包括Instrumentation类,Application类和context类。
其中对Contetext对象的创建是通过其成员函数createAppContext()来创建的,这里传入的参数是类型为LoadedApk的对象(该类用于保存一些和APK相关的信息,如资源文件的位置,JNI库位置等),而这里所代表的的package是名为"android"的framework-res.apk。
static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
return new ContextImpl(null, mainThread,
packageInfo, null, null, false, null, null);
}
public ContextImpl getSystemContext() {
synchronized (this) {
if (mSystemContext == null) {
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}
synchronized (this) {
if (mSystemContext == null) {
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}