Interviewer: What is the application starts
Start the application, and can be called to start the root Activity. But before talking about the application starts, it is necessary for the application process (AppProcess) start understanding, it is because you launch an application must first ensure that the application process has been started. AMS when you start the application, the application process will first check whether there is, if there is no need to request Zygote process to create and start the application process. I will not paste large chunks of code large segment, but some conclusions, and provides links to related source code.
Application process (AppProcess) start
Start Outline
- AMS sends a request to start the application process.
- Zygote receives the request and create the application process.
AMS sends a request to start the application process
-
AMS sends a request to startProcessLocked Zygote process by calling the method.
-
Process invoke
start
method, using ZygoteProcess thestart
method. - In ZygoteProcess the
start
method, has calledstartViaZygote
,zygoteSendArgsAndGetResult
andopenZygoteSocketIfNeeded
so on, and finallyopenZygoteSocketIfNeeded
call the connect method ZygoteState Zygote process of establishing a connection with the method.
AMS sends a request to start the application process
-
ZygoteServer execution
runSelectLoop
method, AMS has been waiting for the arrival of the requested data. -
When AMS request comes after establishing a connection with Zygote process by ZygoteConnection the
processOneCommand
requested data processing method. To request data is parsed to obtain startup parameters program process, and through Zygote'sforkAndSpecialize
created an application process method. -
进程创建完成后,交由ZygoteInit的
zygoteInit
方法和RuntimeInit的applicationInit
方法分别进行进程和应用的初始化。在zygoteInit
方法中,为应用程序进程创建了Binder线程池,这样进程就可以跨进程进行通信了。而applicationInit
方法通过反射最终会调用ActivityThread的main
方法,从而完成应用程序进程的创建。 -
- *
应用程序(App)启动
讲完了应用程序进程(AppProcess)启动的相关内容后,接下来我们就来看看应用程序是如何一步一步启动的。
启动大纲
- Launcher请求AMS。
- AMS请求ApplicationThread。
- ActivityThread启动Activity。
启动时序图
Launcher请求AMS
-
当我们点击应用程序的图标时,就会自动调用Launcher的
startActivitySafely
方法, 最终会调用Activity的startActivity
方法。 -
在Activity的
startActivity
中又调用了startActivityForResult
方法,而startActivityForResult
方法内部又调用了Instrumentation的execStartActivity
方法。 - 在Instrumentation的
execStartActivity
方法中又通过ActivityManager的getService
方法获取了IBinder类型的AMS引用IActivityManager
,最后调用了AMS的startActivity
方法。
AMS请求ApplicationThread
-
在AMS的
startActivity
方法中,又调用了其本身的startActivityAsUser
方法,进行权限的检查。 -
权限检查完后,调用ActivityStarter的
startActivityMayWait
方法,并在该方法中解析处理应用程序需要的参数,并进行相关参数的初始化,最终会调用其startActivity
方法。而在startActivity
方法中又调用了startActivityUnchecked
方法来处理与栈管理相关的逻辑。 -
在处理完栈的关系后,紧接着会调用ActivityStackSupervisor的
resumeFocusedStackTopActivityLocked
方法获取需要启动的Activity所在栈的栈顶。 -
当需要启动的Activity的状态不是RESUMED状态,就需要调用ActivityStack的
resumeTopActivityUncheckedLocked
方法,而它的内部又调用了resumeTopActivityInnerLocked
方法进行一系列的栈状态的判断,最终又回调了ActivityStackSupervisor的startSpecificActivityLocked
方法。 -
在ActivityStackSupervisor的
startSpecificActivityLocked
方法中先是获取了即将启动的Activity所在的应用程序进程(就是在这个地方判断应用所在进程是否存在且已启动,如果没有启动,就需要启动应用程序进程),然后调用realStartActivityLocked
方法。 - 在ActivityStackSupervisor的
realStartActivityLocked
方法中,对启动的应用程序进程进行一系列的判断和处理,最终会调用IBinder类型的ApplicationThread引用IApplicationThread
,通过传入IApplicationThread
建立ClientTransaction
,加入执行LaunchActivityItem
任务,最终实现跨进程执行调用ActivityThread的handleLaunchActivity
方法。
ActivityThread启动Activity
-
在ActivityThread调用了它的
handleLaunchActivity
方法中,会先调用其performLaunchActivity
方法,之后调用handleResumeActivity
,将Activity的状态置为Resume。 - We do a lot of things in performLaunchActivity method ActivityThread in.
- First, the implementation of the
createBaseContextForActivity
method, create a context to launch the Activity; - Secondly, the call is executed Instrumentation
newActivity
way to create Activity instance; - Then, call the LoadedApk the
makeApplication
method to create Application application; - After, Activity need to start calling the
attach
method initializes Activity, Window object is created and associated with the Activity itself. - Finally, the call is executed Instrumentation
callActivityOnCreate
way to start the Activity.
- Instrumentation performed in
callActivityOnCreate
method, calls the Activity of theperformCreate
methods will eventually call the Activity of theonCreate
method, this application will start up.
The application starts the process diagram
Process the application starts, the main processes involved in the Launcher, SystemServer process, Zygote process and the application process of these four processes, the relationship between them is as follows.
to sum up
Well, today's share on here, what if you encounter problems during the interview, or just graduated a few years confused and do not know the work of preparing for interviews and break the status quo upgrade themselves, for their own future is not enough to know I do not know how to plan, I believe you can get simple questions and answers about the relevant surface data packets and video learning.
Bat interview focused on large-scale share knowledge, follow-up will continue to update the hope that through these advanced face interview questions can lower the threshold Android posts, so that more Android Android system engineers to understand and master the Android system. Like I like in trouble clicking a look ~