[FrameWork] ②The four major components are related

Activity related

1. Activity startup process

Inspection point

  • What life cycle callbacks will be experienced when starting Activity
  • The general process of cold start, what components are involved, and what is the communication process like?
  • What is the principle of life cycle callback during Activity startup?

Answer points

  • Starting Activity requires 60 minutes to initiate a binder call to AMS
  • How is the process of Activity started? 80 points
  • Apply the life cycle callback principle of Activity 100 points

The overall process, the steps to start Activity on the application side

  • First load the Activity class in the apk through ClassLoader to generate the Activity object
  • Then prepare the Applicaiton, what actually returns is the Applicaiton that has been created when the application process starts
  • Create Context, which is implemented as ContextImpl
  • Additional context, not only context, but also all important system variables related to the operation of Activity
  • Finally execute the life cycle callback

to sum up:

  • First, the application initiates a startActivity request to AMS,
  • If the application is not started, AMS will initiate a start process request to Zygote
  • After zygote receives the request, it will start the application process
  • After the application process is started, it will initiate an attchApplication IPC call to AMS, mainly using and registering Application Thread
  • Next, AMS will initiate a bindApplication IPC call to the application to initialize the Application
  • After that, AMS initiates a scheduleLaunchActivity IPC call to the application. This call is to create and load an Activity for the application and execute the Activity life cycle

2. Activity display principle

related question

  • Activity display principle (Window/DecorView/ViewRoot)
  • Activity's U refresh mechanism (Vsync/Choreographer)
  • U|'s drawing principle (Measure/Layout/Draw)
  • Surface原理(Surface/SurfaceFlinger)

Answer points

  • What is PhoneWindow and how was it created?
  • What is the principle of setContentView?
  • What is the reason why Activity will be displayed after onResume?
  • What does ViewRoot do, is it the rootView of View Tree?
  • What is the display principle of View? What role does WMS play?

Summary The
code details are shown in Figure 32

  • A PhoneWindow is created when the Activity starts
  • There is a DecorView in the phoneWindow, which is the Root view of the View Tree of the entire Activity
  • ContentView is part of DecorView
  • Decorview will correspond to a ViewRootImpl object, and the ViewRootImpl object can communicate with WMS in both directions
    • ViewRootImpl can initiate a binder call to WMS through IwindowSession
    • WMS initiates a binder call to the application side through IWindow
  • The most important step for Activity to be displayed is the creation of ViewRootImpl, and ViewRootImpl is solely responsible for the drawing of Decorview
  • ViewRootImpl will register a window in WMS. WMS will uniformly manage the size, position and level of all windows. When drawing for the first time, ViewRootImpl will apply to WMS for a Service for drawing
  • After the drawing is completed, SurfaceFlinger will synthesize the surface according to the Window level, size, and position provided in the WMS. After the synthesis is completed, it can be written to the frame buffer of the screen and displayed.
  • As shown in Figure 33

3. How to start the UI thread of the application

Answer points

  • What is a UI thread?
    • The UI thread is the thread where the UI is refreshed
    • The UI is refreshed in a single thread (if it is multi-threaded export and lock, it is easy to cause problems)
  • UI thread startup process, how is the message loop created
  • Understand the principle of Android UI display, how is the UI thread and U| system related?

Activity.runOnUiThread(Runnable)与view.post(Runnable)

  • Activity.runOnUiThread(Runnable)

    • Conclusion 1: For Activity, the UI thread is the main thread
    • Figure 34
  • view.post(Runnable)

    • Conclusion 2: For View, its U thread is the thread where ViewRootlmpl was created!
    • Figure 35
  • The problem of refreshing the UI by the child thread

    • Conclusion: Only the thread that created the view tree can access its child views
    • As shown in Figure 36image
  • Conclusion 3: The ViewRootlmpl corresponding to Activity's DecorVie is created on the main thread

    • As shown in Figure 37

Three conclusions about the UI thread

  • For Activity, the UI thread is the main thread
    • activity.runOnUiThread()
  • For View, the UI thread is the thread where ViewRootlmpl was created
    • View.post(Runnable r)
  • The ViewRootlmpl corresponding to Activity's DecorView is created on the main thread
    • checkThread

Can child threads refresh the UI?
Yes, the code is as follows

new Thread(){
    @Override
    public void run(){
        //因为添加window是IPC操作,回调回来时,需要handler切换线程,所以需要Looper
        Looper.prepare();
        ...
        getWindowManager().addView(view,params);
        //开启looper,循环取消息。
        Looper.loop()
    }
}.start();

The difference between Looper.prepare() and Looper.prepareMainLooper()

  • Looper.prepareMainLooper(), MainLooper is already set after the application process is started
  • prepare(false) means that looper cannot exit. For mainLooper, looper cannot exit. Other threads can exit. Calling looper.quit of the main thread will throw an exception.

to sum up

  • UI thread refers to the thread that refreshes the UI. Since ViewRootImpl is created in the main thread, it actually refers to the main thread
  • The start of the UI thread is actually the start of the main thread, which is divided into three steps
    • ①Zygote fork process
    • ②Start the binder thread
    • ③ Execute the entry function ActivityThread.main()
  • The message loop is opened by the Looper of the main thread

Service related

1. Talk about the startup principle of Service

Inspection point

  • What are the ways to start the service?
    • startService
    • bindService
    • Difference: bindServcie will not trigger onStartCommand
  • What are the main processes in the service startup process?
  • Which participants are involved in the service startup process, and what is the communication process?

Code analysis

The main start-up process

  • If the Service is started, call onStartCommand directly
  • If the service is not started
    • Process started
      • Start Service
      • Call onStartCommand
    • The process did not start
      • Start process
      • Start Service
      • Call onStartCommand

Which participants are involved in the initiation process, and the communication process

  • Process A wants to start a Service and initiates a startService call to AMS

  • If AMS finds that the Service process is not started, it will initiate a request to start the process to the zygote process through the socket

  • The zygote process will start the application process

  • After the application process is started, the ActivityThread main function will be executed

  • Initiate the binder call of attchApplication to AMS in the entry function, and AMS will know that the application process is ready

  • Then AMS will initiate a bindApplication binder call to the application, allowing the application to create its own Application

  • Next, we will start to process some pending application components in this application process, such as Service-related. Here are two consecutive requests, both of which are executed in the main thread.

  • scheduleCreateServices is used to create Service on the application side

  • ScheduleServiceArgs is used to call onStartCommand on the application side

  • As shown in Figure 41

to sum up

  • First answer the starting method, the answer is as above
  • Then answer the main process in the startup process
  • Finally, answer which participants are involved in the service startup process and what is the communication process?

2. Talk about the binding principle of Service

Investigation point :

  • bindService usage
  • Understand the general process of bindServcie
  • What are the participants involved in bindService and what is the communication process?

Usage: Figure 43

Overall process

  • First, the application initiates a bindService call to AMS
  • AMS checks whether there is a binder handle
  • If so, the binder handle will be called back to the application
  • If not, AMS will request a handle from Service, Service will publish the handle to AMS, and then AMS will call back the binder handle to the application
  • After the application gets the binder handle, it can initiate a binder call to the Service

  • When the application side bindService, it will bring an IserviceConnection object
  • This object is a Bindder object, after passing this object to AMS, AMS will save it
  • When the Service is successfully bound, AMS will call the connected function of IserviceConnection to actively notify the application.
  • Then IServiceConnection holds a reference to ServiceConnection, which means that the functions of ServiceConnection can be called
  • It is worth noting that IServiceConnection and ServiceConnection are not necessarily a one-to-one relationship
    • A context and a ServiceConnection form a two-tuple, and this two-tuple has a one-to-one relationship with IserviceConnection
    • This means that when the same ServiceConnection uses a different Context to bind the Service, it will correspond to a different IserviceConnection.
    • Similarly, if the same Context uses a different ServiceConnection to bind this Service, it will also correspond to a different IServiceConnection
    • For AMS, he only recognizes IServiceConnection, as long as IServiceConnection is different, he thinks it is a different Connection

The realization principle of bindService bindService is sent from the application side to AMS, and the processing function in AMS is bindServiceLocked

First look at a set of data structures

  • A Service on the application side corresponds to a ServiceRecord in AMS
  • A ServiceRecord can contain one or more IntentBindRecord
  • An IntentBindRecord can contain one or more AppBindRecord
  • An AppBindRecord can contain one or more ConnectionRecord

Look at the specific process below

  • Starting the Service will eventually call the bringUpServiceLocked function in AMS, and the subsequent process is as follows
  • Figure 46

When is onRebind called

  • Figure 47

unbindService implementation

  • Figure 48

3. Talk about the registration and sending and receiving principles of dynamic broadcasting

Inspection point

  • The registration principle of dynamic broadcasting
  • The transmission principle of broadcast
  • Principles of Broadcast Reception

The registration principle of dynamic broadcasting

  • On the application side

    • Figure 49
  • On the AMS side

    • AMS received a request to register for broadcast on the application side
    • Figure 50

Broadcast transmission

  • Figure 51

Broadcast reception

  • Figure 52image

  • AMS needs to distribute intents to the 5 receivers of the application. Ordinary dynamic broadcasts will be distributed in parallel, but after broadcast to the application process, the distribution is synchronous, that is, serial processing.

    • As shown in Figure 53
  • Let's look at the performReceive implementation of IIntentReceiver

    • As shown in Figure 54

to sum up

  • First, apply A to register broadcast to AMS, generate a binder object, register the binder and intent filter objects to AMS
  • Application B sends a broadcast, and the broadcast carries an intent
  • AMS will find a matching Receiver based on the intent among all registered Receivers, and then start distribution
  • For ordinary dynamic broadcasting, AMS is distributed in parallel, and distributed serially on the application side
  • Find the corresponding broadcast Receiver through the binder object, and then execute its receive function
  • As shown in Figure 55

A brief summary is:

  • Registration broadcast encapsulates a binder object to AMS
  • Find the matching receiver through the broadcast intent, and then distribute
  • Ordinary dynamic broadcasting is distributed in parallel on the system side (AMS) and serially distributed on the application side

4. Talk about the registration and sending and receiving principles of static broadcasting

The registration
of broadcast is different from dynamic broadcast. Static broadcast is registered in the manifest file. When Android starts, it will start the PMS service. This service will scan the installed apk and parse the mainfest file. When the receiver is detected, it will It is added to the receiver list, and the static broadcast is registered in the PMS at this time. When needed, it will be queried from the PMS, as shown in the following figure, Figure 56

Broadcast transmission Static broadcast is distributed serially. If the process is not started, the process needs to be started. If the distribution times out, the broadcast is discarded. Figure 57

Broadcast reception After being distributed to the application, the application creates a broadcast object in the main thread and executes onReceive, but no context is created. The context in onReceive is not the application context, but the contextWrapper with application as mBase. Figure 58

5. Talk about the startup principle of Provider

Inspection point

  • Understand the life cycle of ContentProvider
  • Familiar with the startup process of ContentProvider
  • Familiar with the communication principle of all parties in the Provider startup process

  • First look at the above figure, three processes, application process, AMS process Provider process
  • To add, delete, modify, and check with the Provider, you need to get the Provider's binder object first
    • After the Provider is started, the binder will be published to AMS first
    • When the application makes a request to AMS, AMS returns the Provider's binder to the application
  • Looking at the following figure, the binder object of the Provider is not used, but a Provider instance is created directly in the application process, so that the application does not need to perform cross-process communication when using the Provider to perform additions, deletions, and changes.
  • So how to create a Provider instance in the application process? First, the uid of the application where the Provider is located is the same as the uid of the caller, and one of the following two conditions must be met.
    • The process name of the provider is the same as the process name of the caller
    • Or the Provider specifies the multiProcess flag

to sum up

Let me talk about the situation where the Provider process is not started :As shown in Figure 60

  • First, application A initiates a call to add, delete, modify, and check to Provider. Since application A and Provider are not a process, the binder needs to communicate across processes, so you need to get the binder object that needs to request AMS.
  • Application A requests Provider's binder object from AMS
  • AMS finds that the application process is not started, it will initiate a request to create a Provider process to zygote through socket communication, and then zygote will create a Provider process
  • The first thing the Provider starts is to call attachApplication to report to AMS, and AMS knows that the process is ready
  • AMS calls bindApplication command to Provider. BindApplication has two important tasks, one is to create Application object, and the other is to initialize all providers (install provider, create provider object, call life cycle)
  • Then Provider calls publishContentProvider to publish the binder object to AMS
  • Now that AMS has the Provider's binder object, it will return the binder object to application A
  • Then the application can initiate a call to add, delete, modify, and check to the Provider.

Let's discuss the situation that the Provider process has been started As shown in Figure 61

  • Application A initiates an addition, deletion, modification, and query call to the Provider
  • Application A requests Provider's binder object from AMS
  • If there is no binder object in AMS, AMS calls schedulelnstallProvider to request the binder object from the Provider process
  • Then the Provider will install and initialize the Provider, call the life cycle, and finally publish the binder object to AMS through publishContentProvider
  • Now that AMS has the Provider's binder object, it will return the binder object to application A
  • Then the application can initiate a call to add, delete, modify, and check to the Provider.

If you want to know more about Android-related knowledge, you can click into my [ GitHub project: https://github.com/733gh/GH-Android-Review-master ] to check it out for yourself , and there are many records in it. Android knowledge points. Finally, please give me your support! ! !

Guess you like

Origin blog.csdn.net/dongrimaomaoyu/article/details/114592527