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);