Android开发涉及到的AMS类和ActivityThread类源码解读

Android开发系统启动流程涉及到的AMS类和ActivityThread类

ZygoteInit
	- public static void main(String argv[]) {}
	- forkSystemServer()	//fork系统进程
	- mMethod.invoke(null, new Object[] { mArgs });//反射调用系统进程的那个ActivityThread类systemMain方法
public static ActivityThread systemMain() {	//系统的
	ActivityThread thread = new ActivityThread();
	thread.attach(true, 0);
	return thread;
}

//如果不是系统的ActivityThread,还是调用Looper.loop();进入死循环,也就是为什么我们app不会退出的原因
ActivityThread extends ClientTransactionHandler
public static void main(String[] args) { //普通
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);	
	Looper.loop();
}

//将mAppThread传递给AMS系统进程
ActivityThread.attach(boolean system, long startSeq)//systemMain方法的对应为true
	- mgr.attachApplication(mAppThread, startSeq);
//首先搞清楚,是一部Android设备系统只有一个AMS进程,并不是每个应用有一个AMS进程
//一个app应用都有一个主进程ActivityThread实例,AMS通过ProcessList mProcessList持有每个应用的进程记录ProcessRecord
//ProcessRecord进而持有ApplicationThread代理,ApplicationThread通过H:Handler与ActivityThread通信


//从main进程切换到AMS进程
ActivityManager.getService() 
//从AMS进程切换到main进程,通过ApplicationThread,在通过H:Handler与ActivityThread通信
IApplicationThread mAppThread	


//App启动后反射调用ActivityThread的main方法入口,然后调用attach方法,将IApplicationThread传递给AMS
//ActivityThread类的ApplicationThread干嘛的知道了吧?就是IApplicationThread的代理类,用于和其他进程比如AMS通信的
//1.ActivityThread
private void attach(boolean system, long startSeq) {
  	final IActivityManager mgr = ActivityManager.getService();
	mgr.attachApplication(mAppThread, startSeq);
}
//2.ActivityManagerService() 
final ActivityThread mSystemThread;	//系统进程,和AMS同一个进程的ActivityThread
final ProcessList mProcessList;	//其他进程的进程列表,管理每个进程
private boolean attachApplicationLocked(IApplicationThread thread){
    ProcessRecord app;
    generateApplicationProvidersLocked()//通过PMS获取xml注册的ContentProvider组件
    thread.bindApplication(processName, appInfo, providerList,...)//将providerList传递给主进程
    app.makeActive(thread, mProcessStats);	//持有thread:ApplicationThread
    mProcessList.updateLruProcessLocked(app, false, null);	//更新进程LRU列表
}


//每个应用的Application创建的时机:
//启动Activity的参数ActivityClientRecord r持有packageInfo:LoadedApk持有Application mApplication
//makeApplication方法判断有Application对象直接返回,没有就创建一个
//ActivityThread
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    Application app = r.packageInfo.makeApplication(false, mInstrumentation);
}
Context的实现类是ContextImpl
上一篇:❤️烦恼?头疼?不知所措?Android的ANR问题,一剂药到病除❤️【建议收藏】


下一篇:Android开发源码解读四大组件源码解读简单梳理