Android中常用類(lèi)的創(chuàng)建點(diǎn)

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

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

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