Tencent Featured Interview key issues: Android source code analysis - application launcher

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

  1. AMS sends a request to start the application process.
  2. Zygote receives the request and create the application process.

AMS sends a request to start the application process

Tencent Featured Interview key issues: Android source code analysis - application launcher

  • AMS sends a request to startProcessLocked Zygote process by calling the method.

  • Process invoke startmethod, using ZygoteProcess the startmethod.

  • In ZygoteProcess the startmethod, has called startViaZygote, zygoteSendArgsAndGetResultand openZygoteSocketIfNeededso on, and finally openZygoteSocketIfNeededcall the connect method ZygoteState Zygote process of establishing a connection with the method.

AMS sends a request to start the application process

Tencent Featured Interview key issues: Android source code analysis - application launcher

  • ZygoteServer execution runSelectLoopmethod, 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 processOneCommandrequested data processing method. To request data is parsed to obtain startup parameters program process, and through Zygote's forkAndSpecializecreated an application process method.

  • 进程创建完成后,交由ZygoteInit的zygoteInit方法和RuntimeInit的applicationInit方法分别进行进程和应用的初始化。在zygoteInit方法中,为应用程序进程创建了Binder线程池,这样进程就可以跨进程进行通信了。而applicationInit方法通过反射最终会调用ActivityThread的main方法,从而完成应用程序进程的创建。

    • *

应用程序(App)启动

讲完了应用程序进程(AppProcess)启动的相关内容后,接下来我们就来看看应用程序是如何一步一步启动的。

启动大纲

  1. Launcher请求AMS。
  2. AMS请求ApplicationThread。
  3. ActivityThread启动Activity。

启动时序图

Tencent Featured Interview key issues: Android source code analysis - application launcher

Launcher请求AMS

Tencent Featured Interview key issues: Android source code analysis - application launcher

  • 当我们点击应用程序的图标时,就会自动调用Launcher的startActivitySafely方法, 最终会调用Activity的startActivity方法。

  • 在Activity的startActivity中又调用了startActivityForResult方法,而startActivityForResult方法内部又调用了Instrumentation的execStartActivity方法。

  • 在Instrumentation的execStartActivity方法中又通过ActivityManager的getService方法获取了IBinder类型的AMS引用IActivityManager,最后调用了AMS的startActivity方法。

AMS请求ApplicationThread

Tencent Featured Interview key issues: Android source code analysis - application launcher

  • 在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

Tencent Featured Interview key issues: Android source code analysis - application launcher

  • 在ActivityThread调用了它的handleLaunchActivity方法中,会先调用其performLaunchActivity方法,之后调用handleResumeActivity,将Activity的状态置为Resume。

  • We do a lot of things in performLaunchActivity method ActivityThread in.
  1. First, the implementation of the createBaseContextForActivitymethod, create a context to launch the Activity;
  2. Secondly, the call is executed Instrumentation newActivityway to create Activity instance;
  3. Then, call the LoadedApk the makeApplicationmethod to create Application application;
  4. After, Activity need to start calling the attachmethod initializes Activity, Window object is created and associated with the Activity itself.
  5. Finally, the call is executed Instrumentation callActivityOnCreateway to start the Activity.
  • Instrumentation performed in callActivityOnCreatemethod, calls the Activity of the performCreatemethods will eventually call the Activity of the onCreatemethod, 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.

Tencent Featured Interview key issues: Android source code analysis - application launcher

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 ~

Guess you like

Origin blog.51cto.com/14332859/2450455