[1]android系统启动第一次调用ActivityManagerService中main方法设置系统上下环境,
并保存在 mContext成员变量中
SystemServer启动后会调用ActivityManagerService中的main函数:
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
publicstatic final Context main(int factoryTest){
AThread thr =new AThread();//new了一个ActivityManagerService类保存在成员变量mService中
thr.start();
synchronized (thr){
while(thr.mService == null){
try{
thr.wait();
}catch(InterruptedException e){
}
}
}
ActivityManagerService m = thr.mService;//将AThread中的成员变量mService给ActivityManagerServer成员变量m
mSelf = m; //将ActivityManagerServer对象m 附值给成员变量mSelf
ActivityThread at = ActivityThread.systemMain();//启动一个systemMain ActivityThread
mSystemThread = at;
Context context = at.getSystemContext(); //将systemMain的上下环境给context变量,之后再传给ActivityManagerServer成员变量mContext
m.mContext = context; //附值给mContext
m.mFactoryTest = factoryTest;
m.mMainStack =new ActivityStack(m, context,true);
m.mBatteryStatsService.publish(context);
m.mUsageStatsService.publish(context);
synchronized (thr){
thr.mReady =true;
thr.notifyAll();
}
m.startRunning(null, null, null, null);
return context;
}
[2] ActivityThread.systemMain:
framework/base/core/java/android/app/ActivityThread.java
publicstatic final ActivityThread systemMain(){
ActivityThread thread = new ActivityThread();//调用一个空的构造函数,生成对象,系统线程,此线程在Home Activity创建之前生成.
thread.attach(true);//
return thread;
}
[3]执行attach初始化context
private final void attach(boolean system){
sThreadLocal.set(this);
mSystemThread = system;
if(!system){
ViewRoot.addFirstDrawHandler(new Runnable(){
publicvoid run(){
ensureJitEnabled();
}
});
android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
RuntimeInit.setApplicationObject(mAppThread.asBinder());
IActivityManager mgr = ActivityManagerNative.getDefault();
try{
mgr.attachApplication(mAppThread);
}catch(RemoteException ex){
}
}else{
// Don't set application object here -- if the system crashes,
// we can't display an alert, we just want to die die die.
android.ddm.DdmHandleAppName.setAppName("system_process");
try{
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(); //启动app
}catch(Exception e){
thrownew RuntimeException(
"Unable to instantiate Application():"+ e.toString(), e);
}
}
ViewRoot.addConfigCallback(new ComponentCallbacks(){
publicvoid onConfigurationChanged(Configuration newConfig){
synchronized (mPackages){
// We need to apply this change to the resources
// immediately, because upon returning the view
// hierarchy will be informed about it.
if(applyConfigurationToResourcesLocked(newConfig)){
// This actually changed the resources! Tell
// everyone about it.
if(mPendingConfiguration == null ||
mPendingConfiguration.isOtherSeqNewer(newConfig)){
mPendingConfiguration = newConfig;
queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
}
}
}
}
publicvoid onLowMemory(){
}
});
}
[4]接着看getSystemContext:
public ContextImpl getSystemContext(){
synchronized (this){
if(mSystemContext == null){
ContextImpl context =ContextImpl.createSystemContext(this);
//调用ContextImpl静态方法,生成当前对象的上下环境对象
LoadedApk info = new LoadedApk(this, "android", context, null);
context.init(info, null,this);
context.getResources().updateConfiguration(
getConfiguration(), getDisplayMetricsLocked(false));
mSystemContext = context;
//Slog.i(TAG, "Created system resources " + context.getResources()
// + ": " + context.getResources().getConfiguration());
}
}
return mSystemContext;
}