jvm虛擬機(jī)
在開(kāi)機(jī)時(shí),手機(jī)會(huì)在開(kāi)機(jī)過(guò)程中首先創(chuàng)建一個(gè)zygote進(jìn)程,再由zygote進(jìn)程fork出一個(gè)SystemServer進(jìn)程,zygote進(jìn)程創(chuàng)建時(shí)會(huì)創(chuàng)建一VM,zygote在創(chuàng)建SystemServer時(shí)也會(huì)創(chuàng)建一虛擬機(jī)。SystemServer內(nèi)有一ActivityManagerService,創(chuàng)建app時(shí),ActivityManagerService收到請(qǐng)求,再向zygote發(fā)起請(qǐng)求,zygote再fork出一進(jìn)程,會(huì)再創(chuàng)建一VM。
一般一個(gè)進(jìn)程對(duì)應(yīng)一個(gè)jvm虛擬機(jī)。
System|Application|Activty Context創(chuàng)建點(diǎn)
[->ContextImpl.java]
//System Context ----->ActivityThread#getSystemContext
static ContextImpl createSystemContext(ActivityThread mainThread) {
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread,
packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetricsLocked());
return context;
}
//Application Context--->ActivityThread#attch
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, Display.INVALID_DISPLAY);
}
//Activity Context----->ActivityThread#createBaseContextForActivity
static ContextImpl createActivityContext(ActivityThread mainThread,
LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
return new ContextImpl(null, mainThread, packageInfo, null, null, false,
null, overrideConfiguration, displayId);
}
Instrumentation的初始化
ActivityThread.H.handleMessage.BIND_APPLICATION----->handleBindApplication()------>new Instrumentation()
Instrumentation對(duì)象被賦值給Activity過(guò)程
ActivityThread.performLaunchActivity()
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
...
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
if (r.isPersistable()) {
//調(diào)用activity onCreate生命周期
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
...
return activity;
}
應(yīng)用層系統(tǒng)服務(wù)注冊(cè)
SystemServiceRegistry的靜態(tài)代碼塊中注冊(cè)
registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
new CachedServiceFetcher<AccountManager>() {
@Override
public AccountManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.ACCOUNT_SERVICE);
IAccountManager service = IAccountManager.Stub.asInterface(b);
return new AccountManager(ctx, service);
}});
//重點(diǎn)ActivityManagerService
registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
new CachedServiceFetcher<ActivityManager>() {
@Override
public ActivityManager createService(ContextImpl ctx) {
return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
}});
registerService(Context.ALARM_SERVICE, AlarmManager.class,
new CachedServiceFetcher<AlarmManager>() {
@Override
public AlarmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.ALARM_SERVICE);
IAlarmManager service = IAlarmManager.Stub.asInterface(b);
return new AlarmManager(service, ctx);
}});