Zygote进程
Zygote进程启动之后,调用startSystemServer会启动SystemServer进程
SystemServer进程
在SystemServer类中有一个init2方法,它会创建一个ServerThread线程在它的run方法里面注册了各种各样的Service服务。
ServiceManager相关和如何启动
1、首先先获取IServiceManager(aidl文件)接口,此后所有的调用都是调IServiceManager接口的方法。
启动服务 ServiceManager.addService(name,service),调用的getIServiceManager().addService(name, service)。
2、ServiceManager本身是如何启动的?
它在c层通过getContextObject进行转换而来
sp<IServiceManager> defaultServiceManager()
{
if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
{
AutoMutex _l(gDefaultServiceManagerLock);
if (gDefaultServiceManager == NULL) {
gDefaultServiceManager = interface_cast<IServiceManager>(
ProcessState::self()->getContextObject(NULL));
}
}
return gDefaultServiceManager;
}
SystemServer中启动ActivityManagerService
在SystemServer中调用 context = ActivityManagerService.main(factoryTest)
在main方法里面创建AThread线程并在该线程里面new ActivityManagerService对象,等它创建完成之后赋给变量mSelf变量。
private void main(){
ActivityManagerService m = thr.mService;
mSelf = m;
ActivityThread at = ActivityThread.systemMain();//创建ActivityThread
mSystemThread = at;
Context context = at.getSystemContext(); //获取系统上下文对象
context.setTheme(android.R.style.Theme_Holo);//设置主题
m.mContext = context;
m.mFactoryTest = factoryTest;
m.mMainStack = new ActivityStack(m, context, true);
m.mBatteryStatsService.publish(context);
m.mUsageStatsService.publish(context);
}
在ActivityThread.systemMain()-->attach()里面创建
mInstrumentation = new Instrumentation();
ContextImpl context = new ContextImpl();
context.init(getSystemContext().mPackageInfo, null, this);
Application app = Instrumentation.newApplication(Application.class, context);
mAllApplications.add(app);
mInitialApplication = app;
app.onCreate();
调用ActivityManagerService.setSystemProcess方法
public static void setSystemProcess() {
try {
ActivityManagerService m = mSelf;
ServiceManager.addService("activity", m);
ServiceManager.addService("meminfo", new MemBinder(m));
ServiceManager.addService("gfxinfo", new GraphicsBinder(m));
ServiceManager.addService("permission", new PermissionController(m));
ApplicationInfo info =
mSelf.mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS);
mSystemThread.installSystemApplicationInfo(info);
synchronized (mSelf) {
ProcessRecord app = mSelf.newProcessRecordLocked(
mSystemThread.getApplicationThread(), info,
info.processName);
app.persistent = true;
app.pid = MY_PID;
app.maxAdj = ProcessList.SYSTEM_ADJ;
mSelf.mProcessNames.put(app.processName, app.info.uid, app);
synchronized (mSelf.mPidsSelfLocked) {
mSelf.mPidsSelfLocked.put(app.pid, app);
}
mSelf.updateLruProcessLocked(app, true, true);
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
SystemServiceManager
WindowManagerService
创建WindowManagerService它跟AMS一样,都是通过new创建的。
通过下面代码将AMS和WMS进行关联。
ActivityManagerService.self().setWindowManager(wm);