Android中常用类的创建点

jvm虚拟机

在开机时,手机会在开机过程中首先创建一个zygote进程,再由zygote进程fork出一个SystemServer进程,zygote进程创建时会创建一VM,zygote在创建SystemServer时也会创建一虚拟机。SystemServer内有一ActivityManagerService,创建app时,ActivityManagerService收到请求,再向zygote发起请求,zygote再fork出一进程,会再创建一VM。

一般一个进程对应一个jvm虚拟机。

System|Application|Activty Context创建点

[->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()) {
            //调用activity onCreate生命周期
            mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
        } else {
             mInstrumentation.callActivityOnCreate(activity, r.state);
        }
  ...
  return activity;
}

应用层系统服务注册

SystemServiceRegistry的静态代码块中注册

registerService(Context.ACCOUNT_SERVICE, AccountManager.class,
                new CachedServiceFetcher() {
            @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() {
            @Override
            public ActivityManager createService(ContextImpl ctx) {
                return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
            }});

registerService(Context.ALARM_SERVICE, AlarmManager.class,
                new CachedServiceFetcher() {
            @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);
            }});

你可能感兴趣的:(Android中常用类的创建点)