Application 启动流程
来源:互联网 发布:约瑟夫环数组 编辑:程序博客网 时间:2024/06/05 07:33
本文共分为三个部分
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 }492493 Application app = null;494495 String appClass = mApplicationInfo.className;496 if (forceDefaultAppClass || (appClass == null)) {497 appClass = "android.app.Application";498 }499500 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 " + appClass511 + ": " + e.toString(), e);512 }513 }514 mActivityThread.mAllApplications.add(app);515 mApplication = app;
0 0
- Application 启动流程
- Android Application启动流程分析
- Android Application启动流程分析
- Android Application启动流程分析
- Android Application启动流程分析
- 源码分析之application启动流程
- Android源码(5) --- Application 启动流程
- [译]Android Application启动流程分析
- Replugin Application初始化流程
- android N进程启动流程(二)(上一个activity的暂停、进程启动、绑定进程与创建application)
- 启动流程
- 启动流程
- application 启动定时监听
- cmd scheme 启动application
- Application 启动速度优化
- Application启动过程初探
- Uboot启动流程和Kernel启动流程
- Oracle Application Express启动服务
- 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
- nyoj 1234 签到题
- 数据库面试题1
- UVA - 11604 General Sultan(构图暴力)
- git学习之旅(三)——合并冲突(conflict)
- Application 启动流程
- Servlet通过session进行登录验证
- android 取得设备信息
- 自定义控件(PullToRefreshListView,XListView)的原理
- scala中下划线的几种用法
- 直接插入排序
- C语言register关键字—最快的关键字
- UIView基础
- 内存堆和栈的区别