From 63e4564b086c7edac38415fdc3431db8f814201f Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Thu, 19 Mar 2020 21:54:59 +0800 Subject: [PATCH] =?UTF-8?q?Create=20Application=20=E7=9A=84=E7=90=86?= =?UTF-8?q?=E8=A7=A3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../应用进程相关/Application 的理解.md | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 blogs/Android/Framework/Interview/应用进程相关/Application 的理解.md diff --git a/blogs/Android/Framework/Interview/应用进程相关/Application 的理解.md b/blogs/Android/Framework/Interview/应用进程相关/Application 的理解.md new file mode 100644 index 0000000..c56cf76 --- /dev/null +++ b/blogs/Android/Framework/Interview/应用进程相关/Application 的理解.md @@ -0,0 +1,111 @@ +--- +谈谈你对 Application 的理解 +--- + +1. 了解 Application 的作用 +2. 熟悉 Application 的类继承关系以及生命周期 +3. 深入理解 Application 的初始化原理 + +#### Application 有什么用? + +1. 应用进程初始化操作 +2. 提供上下文 + +#### Application 怎么初始化? + +```java +// ActivityThread#attach +private void attach() { + final IActivityManager mgr = ActivityManagerNative.getDefault(); + mgr.attachAppliation(mAppThread); +} +``` + +```java +// AMS +public final void attachApplication(IApplicationThread thread) { + synchronized(this) { + attachApplicationLocked(thread, callingPid); + } +} +``` + +```java +boolean attachApplicationLocked(IApplicationThread thread, ...) { + //... + // IPC 调用,回到应用进程 + thread.bindApplication(...); +} +``` + +```java +public final void bindApplication(...) { + AppBindData data = new AppBindData(); + //... + sendMessage(H.BIND_APPLICATION, data); +} +``` + +```java +private void handleBindApplication(AppBindData data) { + // LoadedApk + data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo); + // LoadedApk#makeApplication + Application app = data.info.makeApplication(...); + // Application#onCreate + mInstrumentation.callApplicationOnCreate(app); +} +``` + +```java +public Application makeApplication(...) { + if(mApplication != null) { + return mApplication; + } + ContextImpl appContext = ContextImpl.createAppContext(...); + app = mActivityThread.mInstrumentation.newApplication(...); + return app; +} +``` + +```java +Application new Application(ClassLoader cl, String className, Context context) { + return newApplication(cl.loadClass(className), context); +} +``` + +```java +static Application newApplication(Class clazz, Context context) { + Application app = (Application)clazz.newInstance(); + app.attach(context); + return app; +} +``` + +```java +final void attach(Context context) { + attachBaseContext(context); +} +``` + +总结一下 Application 的初始化: + +1. new Application() +2. Application.attachBaseContext() +3. Application.onCreate() + +**不要执行耗时操作** + +```java +boolean attachApplicationLocked(IApplicationThread thread, ...) { + //... + // IPC 调用,回到应用进程 + thread.bindApplication(...); + //... + // 启动待启动的组件 + mStackSupervisor.attachApplicationLocked(...); + mServices.attachApplicationLocked(app, processName); + sendPendingBroadcastsLocked(app); +} +``` +