源码笔记-应用启动流程
标签(空格分隔): android 源码 framwork
1、ActivityManagerService.startProcessLocked()
会执行ActivityThread.main()
方法,去启动应用。
private final void startProcessLocked(ProcessRecord app String hostingType, String hostingName, String abiOverride, String entryPoint, String[] entryPointargs) {
...//忽略
if (entryPoint == null) {
entryPoint = "android.app.ActivityThread";
}
Process.start(entryPoint, ...);
}
2、在ActivityThread.main()
中,会先执行以下几步操作:
1、
Looper.prepareMainLooper()
方法,将当前线程跟Looper
绑定;
2、创建一个新的ActivityThread
实例;
3、执行ActivityThread.attach()
创建application
以及Activity
4、执行Looper.loop()
方法,遍历MessageQueue
ActivityThread.java
public static void main(String[] args) {
...
Looper.prepareMainLooper(); //当前线程跟Looper绑定
ActivityThread thread = new ActivityThread();
thread.attach(false); //false表示非系统应用
Looper.loop();
}
private void attach(boolean system) {
if (!system) {
...
final IActiivtyManager mgr = ActivityManagerNative.getDefault(); //返回ActivityManagerService
try {
mgr.attachApplication(mAppThread);
}
...
}
...
}
3、在ActivityManagerService.attachApplication()
中,会执行attachApplicationLocked()
方法。
ActivityManagerService.java
private final attachApplicationLockded(IApplicationThread thread, int pid) {
...
thread.bindApplication(); //创建Application
mStackSupervisor.attachApplicationLocked(app); //创建并启动Launcher Activity
}
代码中的thread
为ActivityThread.ApplicationThread
的实例,所以接下去查看bindApplication()
的内容;
4、ActivityThread.ApplicationThread.bindApplication()
会通过Handler
发送一个BIND_APPLICATION
的message
,并通过ActivityThread.handleBindApplication()
方法处理
private void handleBindApplication() {
...
Application app = data.info.makeApplication(); //创建Application,并执行attach();
mInstrumentation.callApplicationOnCreate(app);//执行Application的onCreate()
}
5、LoadedApk.makeApplication()
方法会创建返回application
,并且执行application.attach()
方法
LoadedApk.java
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
...
Application app = null;
String appClass = mApplicationInfo.className;
if (forceDefaultAppClass || (appClass == null)) {
appClass = "android.app.Application";
}
...
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);
...
return app;
}
Instrumentation.java
static public Application newApplication(Class> clazz, Context context) {
Application app = (Application)clazz.newInstance();
app.attach(context);
return app;
}
从上面的代码中看到如果使用默认的APPClass
或者appClass
为null
的时候,会使用android.app.Applicaiton
类,并通过反射去实例化Application
,同时会执行app.attach()
方法,所以当我们的manifest
中,Application
标签的name
会默认为android.app.Applicaiton
.
6、看完ActivityThread.bindApplication()
后,我们来看ActivityStackSupervisor.attachApplicationLocked()
;
ActivityStackSuppervisor.java
boolean attachApplicationLocked(ProcessRecord app) {
...
try {
if (realStartActivityLocked(hr, app, true, true)) {
didSomething = true;
}
} cach (RemoteException e) {
}
...
}
final boolean realStartActivityLocked() {
...
app.thread.scheduleLaunchActivity();
...
}
可以看出最终会调用到IApplicationThread
的scheduleLaunchActivity()
。接下来转过去看看该方法的实现。
ActivityThread.java
public final void scheduleLaunchActivity() {
...
sendMessage(H.LAUNCH_ACTIVITY, r); //发送launch Activity的消息
}
private void handleLaunchActivity() {
...
WindowManagerGlobal.initialize();
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
handleResumeActivity();
}
...
}
private Activity performLaunchActivity() {
...
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity();
}
...
activity.attach(); //attach
mInstrumentation.callActivityOnCreate(activity, r.state);//onCreate
activity.performStart(); //执行onStart
...
}
通过代码可以看到,performLaunchActivity()
会通过反射创建Activity
的实例,并先后执行Activity
的attach
、onCreate
、onStart()
等生命周期方法。handleResumeActivity()
会执行Activity
的onResume()
生命周期,到这一步,可以说我们的app
已经启动起来了。从创建进程,到创建application
再到创建launch Activity
。