时间:2020-09-28 22:33:55 | 栏目:Android代码 | 点击:次
注:只是说明启动activity的过程(ActivityThread如何与ActivityManagerService简称AmS进行进程间通信调用全过程),不解析android从zygote(受精卵)到整个系统服务的启动
具体来讲,启动activity的方式有以下几种:
先分析第2种方式
android的HOMe桌面程序(launcher)是android系统启动的第一个应用程序,其他的应用程序安装后,会在launcher上创建一个快捷图标,我们点击桌面上的快捷图标就会启动相应的app
桌面程序Launcher.java(源码基于4.2.2,我没有下载4.2.2,参考网上源码)
在android4.0\packages\apps\Launcher2\src\com\android\launcher2
当点击一个应用图标时会执行一连串流程
-》Launcher.onClick(View v)单击app图标
-》Launcher.startActivitySafely(v,intent,tag)这里比4.0多的一个参数,可能性能优化吧
-》Launcher.startActivity(v, intent,tag)
-》Activity.startActivity(intent,opts.toBundle())
-》Activity.startActivityForResult(intent,-1,options);
到这里直接跳转到第一个问题上来了(直接分析第一个就可以解决第二个)
第4种方式---长按“Home”键,显示出当前任务列表,从中选择一个启动
流程:
public static final int KEYCODE_HOME = 3; PhoneWindowManager.interceptKeyBeforeDispatching()处理长按home事件 showRecentAppsDialog();//弹出近期任务的对话框 RecentApplicationsDialog.onclick.getContext().startActivity(intent);//到这里流程就相同了
这个调用的其实也是第1种的startActivity()。所以1,2,4可以用相同处理流程解析,在后面接绍第1中方式处理流程-----fly
第3种方式(原理与第1种大致相同)
假设一个app,ActivityA启动ActivityB,然后ActivityB按下"BACK"键其实执行的是activity的finish()方法
简单流程:
ActivityB.finish()
Activity.finish()
ActivityManagerNative.getDefault().finishActivity()
ActivityManagerService.finishActivity()
ActivityStack.requestFinishActivityLocked()
ActivityStack.finishActivityLocked()
ActivityStack.startPausingLocked()
ActivityB向AmS发送finish()请求
// If the activity is PAUSING, we will complete the finish once
// it is done pausing; else we can just directly finish it here.
上面解释。AmS会先会在ActivityStack.finishActivityLocked()方法中检查我们要finish的activity的状态是否处于pause状态,如果是将直接执行finish操作,否则,必须先执行startPausingLocked()---这里终点是resume恢复上一个ActivityA,将A显示在前台窗口
IApplicationThread.schedulePauseActivity()
ActivityThread.schedulePauseActivity()
ActivityThread.sendMessage()
ActivityThread.H.sendMessage()
ActivityThread.H.handleMessage()
ActivityThread.handlePauseActivity()
ActivityThread.performPauseActivity()
Instrumentation.callActivityOnPause()
Activity.performPause()
Activity.onPause()
ActivityManagerNative.getDefault().activityPaused()
ActivityManagerService.activityPaused()
ActivityStack.activityPausedLocked()
ActivityStack.completePauseLocked()
接上面,AmS通知当前ActivityB进入Paused状态,当ActivityB进入paused状态后即Activity.onPause()方法执行完后,通知AmS我已经执行完pause操作。于是AmS就准备要在ActivityB所在的进程和任务中恢复ActivityA了;
ActivityStack.resumeTopActivityLocked()
ActivityStack.resumeTopInnerLocked()
IApplicationThread.scheduleResumeActivity()
ActivityThread.scheduleResumeActivity()
ActivityThread.sendMessage()
ActivityTherad.H.sendMessage()
ActivityThread.H.handleMessage()
ActivityThread.H.handleResumeActivity()
Activity.performResume()
Activity.performRestart()
Instrumentation.callActivityOnRestart()
Activity.onRestart()
Activity.performStart()
Instrumentation.callActivityOnStart()
Activity.onStart()
Instrumentation.callActivityOnResume()
Activity.onResume()
到这里activityA已经启动起来了,但是ActivityB还没有被finish掉,在ActivityThread.H.handleResumeActivity中会
调用Looper.myQueue().addIdleHandler(new Idler()) 这个方法实现ActivityB的最终销毁操作
Looper.myQueue().addIdleHandler(new Idler())
ActivityManagerNative.getDefault().activityIdle()
ActivityManagerService.activityIdle()
ActivityStackSupervisor.activityIdleInternalLocked()
ActivityStack.destroyActivityLocked()
IApplicationThread.scheduleDestoryActivity()
ActivityThread.scheduleDestoryActivity()
ActivityThread.sendMessage()
ActivityThread.H.sendMessage()
ActivityThread.H.handleMessage()
ActivityThread.handleDestoryActivity()
ActivityThread.performDestoryActivity()
Activity.performStop()
Instrumentation.callActivityOnStop()
Activity.onStop()
Instrumentation.callActivityOnDestory()
Activity.performDestory()
Acitivity.onDestory()
ActivityManagerNative.getDefault().activityDestoryed()
ActivityManagerService.activityDestoryed()
ActivityStack.activityDestoryedLocked()
这就是finish()的全部流程了(具体与WindowsManagerService的交互以后再补充)
第1种方式--ActivityA启动ActivityB为例
从startActivity()开始分析。
简单流程(有时间完整过一遍源码)
Activity.startActivity
Activity.startActivityForResult
Instrumentation.execStartActivity
ActivityManagerProxy.startActivity
ActivityManagerService.startActivity
ActivityStack.startActivityMayWait
ActivityStack.startActivityLocked
ActivityStack.startActivityUncheckedLocked
ActivityStack.resumeTopActivityLocked
ActivityStack.startPausingLocked
ApplicationThreadProxy.schedulePauseActivity
ApplicationThread.schedulePauseActivity
ActivityThread.queueOrSendMessage
H.handleMessage
ActivityThread.handlePauseActivity
ActivityManagerProxy.activityPaused
ActivityManagerService.activityPaused
ActivityStack.activityPaused
ActivityStack.completePauseLocked
ActivityStack.resumeTopActivityLokced
ActivityStack.startSpecificActivityLocked
ActivityStack.realStartActivityLocked
ApplicationThreadProxy.scheduleLaunchActivity
ApplicationThread.scheduleLaunchActivity
ActivityThread.queueOrSendMessage
H.handleMessage
ActivityThread.handleLaunchActivity
ActivityThread.performLaunchActivity
AcitiviyB.onCreate
要查看ActivityManagerNative.java,ActivityManagerProxy.java,ActivityManagerService,还有binder关系看一张图就可以了
从图中可以看出代理类:使用ActivityManagerProxy代理类,来代理ActivityManagerNative类的子类ActivityManagerService;
所以执行请求都是传递到ActivityManagerService进行处理