本文共分为三个部分
1.Application的父类关系
2.Application在什么时候创建到
3.Application的Context是在什么时候创建到,
1.Application的关系图还是比较简单的,画个图大家来了解一下即可。
2.Application是在什么时候创建的,
最近看到一篇文章说application是在Bind Applicationd的时候创建的,这只是四种创建方式之一。
下面我们来详细的了解一下其他的启动方式:
1.activity启动的时候,会创建Application
2.broadcast接受的时候,会创建Application
3.Service启动的时候,会创建Application
4.bindApplication的时候,会创建application(这个可能大家会疑惑时什么时候,稍后详细介绍)
首先来介绍前公共流程:
对于前三个,很容易理解,但第四个是什么时候呢,下面我们来专门对第四个来分析一下,
step1:SystemServer.java
@Override public void run() {
.......
Slog.i(TAG, "Activity Manager"); context = ActivityManagerService.main(factoryTest);.......
}
step2 : ActivityManagerService.java
public static final Context main(int factoryTest) { AThread thr = new AThread(); thr.start();
ActivityThread at = ActivityThread.systemMain();}
step 3 : ActivityThread.java
public static ActivityThread systemMain() { 5022 HardwareRenderer.disable(true); 5023 ActivityThread thread = new ActivityThread(); 5024 thread.attach(true); 5025 return thread; 5026 }
step 4 : ActivityManagerService.java
public final void attachApplication(IApplicationThread thread) { synchronized (this) { int callingPid = Binder.getCallingPid(); final long origId = Binder.clearCallingIdentity(); attachApplicationLocked(thread, callingPid); Binder.restoreCallingIdentity(origId); } }
private final boolean attachApplicationLocked(IApplicationThread thread, int pid) {
......
thread.bindApplication(processName, appInfo, providers, app.instrumentationClass, profileFile, profileFd, profileAutoStop, app.instrumentationArguments, app.instrumentationWatcher, app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), mCoreSettingsObserver.getCoreSettingsLocked());......
}
step5 : ActivityThread.java
public final void bindApplication(String processName, ApplicationInfo appInfo, List<ProviderInfo> providers, ComponentName instrumentationName, String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler, Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher, IUiAutomationConnection instrumentationUiConnection, int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent, Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
......
queueOrSendMessage(H.BIND_APPLICATION, data);
}
case BIND_APPLICATION: 1314 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication"); 1315 AppBindData data = (AppBindData)msg.obj; 1316 handleBindApplication(data);
到这里我们可以清理的了解到,这套流程是最系统启动的时候就完成的。
第三部分:Application的Context是什么时候创建的
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) { if (mApplication != null) { return mApplication; } Application app = null; String appClass = mApplicationInfo.className; if (forceDefaultAppClass || (appClass == null)) { appClass = "android.app.Application"; } try { java.lang.ClassLoader cl = getClassLoader(); ContextImpl appContext = new ContextImpl(); appContext.init(this, null, mActivityThread); app = mActivityThread.mInstrumentation.newApplication( cl, appClass, appContext); appContext.setOuterContext(app); } catch (Exception e) { if (!mActivityThread.mInstrumentation.onException(app, e)) { throw new RuntimeException( "Unable to instantiate application " + appClass + ": " + e.toString(), e); } } mActivityThread.mAllApplications.add(app); mApplication = app;
......
}
这里的mApplication就是我们所要获取的Context,
当我们调用getApplicationContext返回的就是这个对象。
谢谢!
215 Slog.i(TAG, "Activity Manager"); 216 context = ActivityManagerService.main(factoryTest);
1462 public static final Context main(int factoryTest) { 1463 AThread thr = new AThread(); 1464 thr.start(); 1465
487 public Application makeApplication(boolean forceDefaultAppClass, 488 Instrumentation instrumentation) { 489 if (mApplication != null) { 490 return mApplication; 491 } 492 493 Application app = null; 494 495 String appClass = mApplicationInfo.className; 496 if (forceDefaultAppClass || (appClass == null)) { 497 appClass = "android.app.Application"; 498 } 499 500 try { 501 java.lang.ClassLoader cl = getClassLoader(); 502 ContextImpl appContext = new ContextImpl(); 503 appContext.init(this, null, mActivityThread); 504 app = mActivityThread.mInstrumentation.newApplication( 505 cl, appClass, appContext); 506 appContext.setOuterContext(app); 507 } catch (Exception e) { 508 if (!mActivityThread.mInstrumentation.onException(app, e)) { 509 throw new RuntimeException( 510 "Unable to instantiate application " + appClass 511 + ": " + e.toString(), e); 512 } 513 } 514 mActivityThread.mAllApplications.add(app); 515 mApplication = app;