几个重要的类:
ActivityThread:
App启动的真正入口。当Zygote进程孵化出一个新的应用进程后,后执行ActivityThread的main方法。Instrumentation:
它用来监控应用程序和系统的交互。真正做事的是ActivityManagerNative.getDefault()。-
ActivityManagerService(AMS)、ActivityManagerNative、 ActivityManagerProxy、ActivityManager:
1. ActivityManagerNative是一个抽象类继承Binder,实现IActivityManger接口
public abstract class ActivityManagerNative extends Binder implements IActivityManager
gDefault.get()返回的就是IActivityManager,如果实在客户端调用就是返回的ActivityManagerProxy对象。2. ActivityManagerProxy是ActivityManagerNative同文件的类,不是内部类,两个类都在同一个文件中, 实现IActivityManager接口 3. ActivityManagerService继承了ActivityManagerNative类 ActivityMangerProxy远程调用方法后,先到ActivityManagerService类中的onTransact方法,该方法中调用 super.onTransact调用父类ActivityManagerNative的onTransact方法,最终都会调用ActivityManagerService中的方法 4. ActivityManager作为中介来访问IActivityManager提供的功能。 ActivityManager是通过ActivityManagerNative.getDefault()来获取到IActivityManager这个接口的。 因为ActivityManager是运行在用户进程的,因而getDefault()获取的是ActivityManagerProxy.
ApplicationThread,ApplicationThreadNative,IApplicationThread:
ApplicationThread是ActivityThread的内部类,继承ApplicationThreadNative;
ApplicationThreadNative抽象类继承Binder实现IApplicationThread;
用于和AMS进行通信。ApplicationThread通过binder与Ams通信,并将Ams的调用,
通过H类(也就是Hnalder)将消息发送到消息队列,然后进行相应的操作。
ActivityThread的main方法:
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
Looper.loop();
ActivityThread的attach方法: 只写主要的代码
final IActivityManager mgr = ActivityManagerNative.getDefault();
//这个mgr的对象是ActivityThreadProxy,最终调用的是AMS中的方法
try {
//mAppThread是ApplicationThread的对象
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
ActivityMangerService的attachApplication:
attachApplication():
attachApplicationLocked(thread, callingPid);
//通过binder,跨进程调用ApplicationThread的bindApplication()方法, 下面代码逻辑重回ActivityThread.java
thread.bindApplication();
mStackSupervisor.attachApplicationLocked(app)
realStartActivityLocked();//启动Activity
ApplicationThread的bindApplication:
bindApplication():
sendMessage(H.BIND_APPLICATION, data);
//通过mH把BIND_APPLICATION消息发给H处理
//使用H的目的是,把代码执行的逻辑从binder线程池里的线程切换到main线程里去执行.
mH.sendMessage(msg);
ActivityThread中的H
private class H extends Handler {
...
public static final int BIND_APPLICATION = 110;
...
public void handleMessage(Message msg) {
switch (msg.what) {
...
case BIND_APPLICATION:
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
AppBindData data = (AppBindData)msg.obj;
handleBindApplication(data);//调用ActivityThread的handleBindApplication()方法处理
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
break;
...
}
}
}
ActivityThread中的handleBindApplication
// 以下两个方法都是设置进程名称
Process.setArgV0(data.processName);
//可以通过反射DdmHandleAppName类中的getAppName方法获取进程名称
android.ddm.DdmHandleAppName.setAppName(data.processName,UserHandle.myUserId());
data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
//创建app运行时的上下文对象,并对其进行初始化.
final ContextImpl appContext = new ContextImpl();
appContext.init(data.info, null, this);
//这里的data.info是LoadedApk类的对象
//在这里创建了上层开发者的代码中所涉及的Applicaiton类的对象
Application app = data.info.makeApplication(data.restrictedBackupMode, null);
...
//如果有ContentProvider的话, 先加载ContentProvider,后调用Application的onCreate()方法
List providers = data.providers;
if (providers != null) {
installContentProviders(app, providers);
}
//调Application的生命周期函数 onCreate()
mInstrumentation.callApplicationOnCreate(app);
// LoadedApk.java
public final class LoadedApk {
...
public Application makeApplication(boolean forceDefaultAppClass,
Instrumentation instrumentation) {
Application app = null;
app = mActivityThread.mInstrumentation.newApplication(
cl, appClass, appContext);
return app;
}
...
}
// Instrumentation.java
public class Instrumentation {
...
public void callApplicationOnCreate(Application app) {
app.onCreate();
}
...
}