参考资料
凯子哥带你学Framework-Activity启动过程全解析
框架层理解Activity生命周期
CSDN Activity.startActivity流程简介
LoopJing的framework
目录
- 1)Activity的工作过程
- 2)Service的工作过程
1)Activity的工作过程
- Activity启动过程调用链(调用举例:Launcher启动Activity)
Launcher.startActivitySafely->
Launcher.startActivity->
//要求在新的Task中启动此Activity
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Activity.startActivity->
Activity.startActivityForResult->
//当startActivityForResult()调用之后实际还是调用Instrumentation
Instrumentation.execStartActivity->
// ********通过Binder通知AMS准备启动Activity********
// ActivityManagerNative.getDefault()返回AMS Proxy接口
///class ActivityManagerProxy implements IActivityManager{}
//public final class ActivityManagerService extends ActivityManagerNative{}
//public abstract class ActivityManagerNative extends Binder implements IActivityManager{}
ActivityManagerNative.getDefault().startActivity->
ActivityManagerProxy.startActivity->
ActivityManagerService.startActivity-> (AMS)
ActivityManagerService.startActivityAsUser->
ActivityStack.startActivityMayWait->
ActivityStack.resolveActivity(获取ActivityInfo)
//aInfo.name为main Activity,如:com.my.test.MainActivity
//aInfo.applicationInfo.packageName为包名,如com.my.test
ActivityStack.startActivityLocked->
//ProcessRecord callerApp; 调用者即Launcher信息
//ActivityRecord sourceRecord; Launcher Activity相关信息
//ActivityRecord r=new ActivityRecord(...),将要创建的Activity相关信息
ActivityStack.startActivityUncheckedLocked->
//Activity启动方式:ActivityInfo.LAUNCH_MULTIPLE/LAUNCH_SINGLE_INSTANCE/
// ActivityInfo.LAUNCH_SINGLE_TASK/LAUNCH_SINGLE_TOP)
// 创建一个新的task,即TaskRecord,并保存在ActivityRecord.task中
//r.setTask(new TaskRecord(mService.mCurTask, r.info, intent), null, true)
// 把新创建的Activity放在栈顶
ActivityStack.startActivityLocked->
ActivityStack.resumeTopActivityLocked->
ActivityStack.startPausingLocked (使Launcher进入Paused状态)->
// ********AMS通过Binder通知Launcher进入Paused状态********
ApplicationThreadProxy.schedulePauseActivity->
//private class ApplicationThread extends ApplicationThreadNative {}
//public abstract class ApplicationThreadNative extends Binder implements IApplicationThread{}
//class ApplicationThreadProxy implements IApplicationThread {}
ApplicationThread.schedulePauseActivity->
ActivityThread.queueOrSendMessage->
// 调用Activity.onUserLeaveHint
// 调用Activity.onPause
// 通知activity manager我进入了pause状态
ActivityThread.handlePauseActivity->
// ****Launcher通过Binder告诉AMS,它已经进入Paused状态 ****
ActivityManagerProxy.activityPaused->
ActivityManagerService.activityPaused->
ActivityStack.activityPaused->(把Activity状态修改为PAUSED)
ActivityStack.completePauseLocked->
ActivityStack.resumeTopActivityLokced->
//***AMS创建一个新的进程,用来启动一个ActivityThread实例***
//***即将要启动的Activity就是在这个ActivityThread实例中运行***
ActivityStack.startSpecificActivityLocked->
ActivityManagerService.startProcessLocked->
// 启动一个新的进程
// 新的进程会导入android.app.ActivityThread类,并且执行它的main函数,
// 即实例化ActivityThread, 每个应用有且仅有一个ActivityThread实例
Process.start("android.app.ActivityThread",...)->
//通过socket向zygote进程发送创建新进程的请求, 通过zygote机制fork创建一个新的进程
Process.startViaZygote->
// 这个函数在进程中创建一个ActivityThread实例,然后调用
// 它的attach函数,接着就开启消息循环,创建ActivityThread.H(Handler) 用于接收ApplicationThread发送的消息
ActivityThread.main->
//首先获取AMS在客户端的代理,ActivityManagerNative.getDefault()[方法内部从ServiceManager.getService("activity"),然后将此Binder asInstance为IActivityManager]
//然后***ActivityThread通过Binder将一个ApplicationThread类的Binder对象传递给AMS,以便AMS通过此Binder对象来控制Activity整个生命周期 ***
ActivityThread.attach->
IActivityManager.attachApplication(mAppThread)->
ActivityManagerProxy.attachApplication->
ActivityManagerService.attachApplication->
// 把在ActivityManagerService.startProcessLocked中创建的ProcessRecord取出来
ActivityManagerService.attachApplicationLocked->
//***AMS通过Binder通知ActivityThread一切准备OK,它可以真正启动新的Activity了 ***
ActivityStack.realStartActivityLocked->
ApplicationThreadProxy.scheduleLaunchActivity->
ApplicationThread.scheduleLaunchActivity->
ActivityThread.handleLaunchActivity-> {
// 加载新的Activity类,并执行它的onCreate
ActivityThread.performLaunchActivity
/*1) Instrumentation.newActivity: 加载新类,即创建Activity对象;
2) ActivityClientRecord.packageInfo.makeApplication:创建Application对象;
<LoadedApk.makeApplication>
3) Activity.attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config):
把Application attach到Activity, 即把Activtiy相关信息设置到新创建的Activity中
4) Instrumentation.callActivityOnCreate:调用onCreate;*/
// 使用Activity进入RESUMED状态,并调用onResume
`ActivityThread.handleResumeActivity }
- 总结:
- 客户端打开Activity通过Binder通知AMS准备启动
- AMS通过Binder通知当前Activity进入Paused状态
- 当前Activity通过Binder告诉AMS已准备完毕
- AMS判断目标Activity对象是否存在:
- 存在则直接ActivityThread.handleResumeActivity
- 不存在,则判断Activity所在进程是否存在:
- 进程不存在:创建进程,实例化ActivityThread,启动Activity
- 进程存在:在该进程中ActivityThread.handleLaunchActivity
2)Service的工作过程
http://www.jianshu.com/p/05b5e4cfb5d9