Android中常用類的創(chuàng)建點

jvm虛擬機(jī)

在開機(jī)時,手機(jī)會在開機(jī)過程中首先創(chuàng)建一個zygote進(jìn)程,再由zygote進(jìn)程fork出一個SystemServer進(jìn)程,zygote進(jìn)程創(chuàng)建時會創(chuàng)建一VM,zygote在創(chuàng)建SystemServer時也會創(chuàng)建一虛擬機(jī)。SystemServer內(nèi)有一ActivityManagerService,創(chuàng)建app時,ActivityManagerService收到請求,再向zygote發(fā)起請求,zygote再fork出一進(jìn)程,會再創(chuàng)建一VM。

一般一個進(jìn)程對應(yīng)一個jvm虛擬機(jī)。

System|Application|Activty Context創(chuàng)建點

[->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對象被賦值給Activity過程

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ù)注冊

SystemServiceRegistry的靜態(tài)代碼塊中注冊

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);
            }});
//重點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);
            }});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容