The Activity related to the

1. Start the Activity process

KaoChaDian

  • What lifecycle callbacks the Activity goes through when it starts
  • General process of cold start, what components are involved, and what is the communication process?
  • How does the lifecycle callback work during an Activity launch?

Answer the questions on the point

  • Launching an Activity requires a binder call to AMS for 60 minutes
  • How is the process in which the Activity is started? 80 points
  • Apply the Activity lifecycle callback principle 100 points

The overall flow, the steps to start an Activity on the application side

  • First, load the Activity class in APK by ClassLoader and generate the Activity object
  • You then prepare appliciton, which actually returns the appliciton that was created when the application process started
  • Create a Context, which is ContextImpl
  • Attach context, including not only the context, but also all important system variables related to the Activity running
  • Finally, the lifecycle callback is performed

Conclusion:

  • First the application makes a startActivity request to AMS,
  • If the application is not started, AMS sends a start process request to Zygote
  • Zygote receives the request and starts the application process
  • After the Application process is started, it makes an IPC call to AMS to attchApplication, using and registering Application Threads
  • AMS then makes a bindApplication IPC call to the Application to initialize the Application
  • AMS then makes a scheduleLaunchActivity IPC call to the application, which creates and loads activities for the application and executes the Activity lifecycle

2. Activity display principle

Issues related to

  • According to principle of Activity (Windows/DecorView/ViewRoot)
  • Activity U Refresh mechanism (Vsync/Choreographer)
  • U | drawing principle (Measure/Layout/Draw)
  • Surface Principles (Surface/SurfaceFlinger)

Answer the questions on the point

  • What is a PhoneWindow and how was it created?
  • How does setContentView work?
  • What is the reason why activities are displayed after onResume?
  • What is ViewRoot? Is it a View Tree rootView?
  • How does a View display work? What role does WMS play?

conclusion

The code details are shown in Figure 32

  • A PhoneWindow is created when the Activity starts
  • The phoneWindow has a DecorView, which is the Root View of the View Tree for the entire Activity
  • The ContentView is part of the DecorView
  • The Decorview corresponds to a ViewRootImpl object that can communicate bidirectionally with the WMS
    • ViewRootImpl can make binder calls to WMS through IwindowSession
    • WMS makes binder calls to the application through IWindow
  • The most important step for an Activity to be displayed is the creation of the ViewRootImpl, which is solely responsible for the Decorview drawing
  • The ViewRootImpl will register a window with WMS. WMS will manage the size, location and hierarchy of all Windows. On the first drawing, the ViewRootImpl will request a Service from WMS to draw
  • After drawing, SurfaceFlinger will synthesize the surface according to the Window level, size and position provided by WMS, and then write it to the frame buffer of the screen for display
  • As shown in figure 33

3. How does the application’S UI thread start

Answer the questions on the point

  • What is a UI thread?
    • The UI thread is the thread that refreshes the UI
    • The UI is refreshed by a single thread (locking can cause problems if the export is multithreaded)
  • UI thread startup process, how is the message loop created
  • Understand the Android UI display principle, the UI thread and U | how connection between system?

Activity. The runOnUiThread (Runnable) and the post (Runnable)

  • Activity.runOnUiThread(Runnable)
    • Conclusion 1: The UI thread is the main thread for an Activity
    • Figure 34
  • view.post(Runnable)
    • Conclusion 2: For a View, its U thread is the thread on which the ViewRootlmpl was created!
    • Figure 35
  • Problem with child thread refreshing UI
    • Conclusion: Only the thread that created the view tree can access its child views
    • As shown in figure 36
  • Conclusion 3: The ViewRootlmpl corresponding to the Activity DecorVie is created on the main thread
    • As shown in figure 37

Three conclusions about UI threads

  • For an Activity, the UI thread is the main thread
    • Activity. The runOnUiThread ()
  • For a View, the UI thread is the thread in which the ViewRootlmpl was created
    • View.post (Runnable r)
  • The ViewRootlmpl corresponding to the 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(){// Looper looper.prepare (); . getWindowManager().addView(view,params); // Start looper to loop through messages. Looper.loop() } }.start();Copy the code

Prepare.prepare () differs from looper.prepareMainLooper ()

  • PrepareMainLooper (), MainLooper is set up after the application process starts
  • Prepare (false) indicates that looper cannot exit. For mainLooper, looper cannot exit. Other threads are allowed to exit

conclusion

  • The UI thread refers to the thread that refreshes the UI, and since the ViewRootImpl is created in the main thread, it actually refers to the main thread
  • Starting the UI thread is essentially starting the main thread in three steps
    • 1 the Zygote fork process
    • Start the binder thread
    • Activitythread.main ()
  • The message loop is opened through the main thread’s Looper

The Service related

1. Describe the startup mechanism of Service

KaoChaDian

  • How can a service be started?
    • startService
    • bindService
    • Difference: bindServcie does not trigger onStartCommand
  • What are the main processes in service startup?
  • What are the participants involved in the service startup process, and what is the communication process?

The code analysis

The main startup process

  • If the Service is started, onStartCommand is called directly
  • If the Service is not started
    • Process started
      • Start the Service
      • Call onStartCommand
    • The process is not started.
      • Start the process
      • Start the Service
      • Call onStartCommand

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

  • Process A initiates A startService call to AMS to start A Service

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

  • The Zygote process starts the application process

  • Once the application process starts, the ActivityThread main function is executed

  • AMS knows that the application process is ready by calling THE Binder attchApplication to AMS from the entry function

  • AMS then makes a binder call to bindApplication to the Application to create its own Application

  • The next step is to process pending application components in the application process, such as Service, where two requests are sent in a row, both of which are executed in the main thread

  • ScheduleCreateServices is used to create a Service on the application

  • CheduleServiceArgs is used to call onStartCommand on the application side

  • As shown in figure 41

conclusion

  • First answer the boot mode, answer above
  • Then answer the main process in the startup process
  • Which participants are involved in the service startup process? What is the communication process like?

2. Describe the binding principle of Service

KaoChaDian:

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

Usage:Figure 43

The overall process

  • The application first makes a bindService call to AMS
  • AMS checks for binder handles
  • If so, the binder handle is called back to the application
  • If not, AMS requests a handle from the Service, which publishes the handle to AMS, which then calls back the binder handle to the application
  • With a Binder handle, the application can make a Binder call to the Service

  • The bindService application takes an IserviceConnection object
  • This object is a Bindder object that AMS saves when it passes to AMS
  • When the Service binding is successful, AMS calls IserviceConnection’s Connected function to actively notify the application
  • IServiceConnection then holds a reference to ServiceConnection, which means that ServiceConnection’s functions can be called
  • It is worth noting that IServiceConnection and ServiceConnection do not necessarily have a one-to-one relationship
    • A context and a ServiceConnection form a tuple that has a one-to-one relationship with IserviceConnection
    • This means that when the same ServiceConnection binds the Service to a different Context, it will correspond to a different IserviceConnection
    • Similarly, if the same Context is bound to a Service with a different ServiceConnection, it will correspond to a different IServiceConnection
    • For AMS, he only recognizes IServiceConnection. As long as IServiceConnection is different, he thinks it is a different Connection

BindService is sent from the application to AMS, where the handler function is bindServiceLocked

Let’s start with a set of data structures

  • An application Service corresponds to a ServiceRecord in AMS
  • A ServiceRecord can in turn contain one or more IntentBindRecords
  • An IntentBindRecord can in turn contain one or more AppBindRecords
  • An AppBindRecord can also contain one or more ConnectionRecords

Let’s look at the specific process

  • Starting the Service will eventually call the bringUpServiceLocked function in AMS, and the flow from there is shown below
  • Figure 46

When is onRebind called

  • Figure 47

UnbindService implementation

  • Figure 48

3. Talk about the registration and transceiver principles of dynamic broadcast

KaoChaDian

  • The registration principle of dynamic broadcast
  • The transmission principle of broadcast
  • The reception principle of broadcast

The registration principle of dynamic broadcast

  • On the application end
    • Figure 49
  • The AMS terminal
    • AMS received a request from the application to register broadcast
    • Figure 50

Transmission of a broadcast

  • Figure 51

Reception of broadcast

  • Figure 52
  • AMS is distributed to the intent’s five application receivers. Normal dynamic broadcast is distributed in parallel, but after broadcast to the application process, distribution is synchronous, i.e. serial processing
    • As shown in figure 53
  • Let’s look at the performReceive implementation of IIntentReceiver
    • As shown in figure 54

conclusion

  • A is used to register A broadcast with AMS, generate A binder object, and register A binder and intent filter object with AMS
  • Application B sends a broadcast with an intent
  • AMS then finds a match based on the intent among all registered receivers and starts distributing
  • For ordinary dynamic broadcast, AMS is distributed in parallel and in serial
  • Find the corresponding broadcast Receiver through the Binder object and execute its receive function
  • As shown in figure 55

A brief summary is as follows:

  • Register broadcast encapsulates a Binder object to AMS
  • A matching receiver is found by broadcasting the intent and then distributed
  • Ordinary dynamic broadcast is distributed in parallel on the system side (AMS) and in serial on the application side

4. Talk about the registration and transceiver principles of static broadcast

Registration of broadcasts

Unlike dynamic broadcast, static broadcast is registered in a manifest file. When Android starts up, it launches the PMS service, which scans the installed APK, parses the Mainfest file, and when it detects a Receiver, adds it to the list of Receivers. Static broadcast is registered in THE PMS at this time. When needed, it will be queried from the PMS, as shown in Figure 56 below

Transmission of a broadcastStatic broadcasts are distributed serially, and if the process is not started, the process needs to be started. If the distribution times out, the broadcast is discarded as shown in Figure 57

Reception of broadcastOnce it’s distributed to the application, the application creates a broadcast object in the main thread, executes onReceive, but it doesn’t create the context, the context in onReceive is not the context of the application, Instead, it is contextWrapper with Application as mBase. Figure 58

5. Describe the startup mechanism of the Provider

KaoChaDian

  • Understand the life cycle of the ContentProvider
  • Be familiar with the startup process of ContentProvider
  • Be familiar with the communication mechanism during Provider startup

  • First look at the figure above, three processes, application process, AMS process Provider process
  • The application uses the Provider to add, delete, change, and check the binder object of the Provider
    • Binder is first released to AMS after Provider launches
    • When the application requests AMS, AMS returns the Provider’s binder to the application
  • As you can see in the figure below, the Binder objects of the Provider are not used. Instead, a Provider instance is created directly in the application process, which eliminates the need for cross-process communication when the application uses the Provider for adding, deleting, modifying, or checking
  • How do you create a Provider instance in an application process when the uid of the Provider application is the same as that of the caller, and one of the following conditions is met
    • The Provider’s process name is the same as the caller’s process name
    • Or the Provider specifies the multiProcess flag

conclusion

The Provider process is not started:

  • Firstly, application A initiates the call of adding, deleting, changing and checking to the Provider. Since application A and Provider are not the same process, binder needs to communicate with each other across processes, so binder objects that need to request AMS need to be obtained
  • Application A requests the Provider’s Binder object to AMS
  • When AMS finds that the application process is not started, it sends a request to Zygote to create the Provider process through socket communication, and Then Zygote creates the Provider process
  • The first thing the Provider does when it starts is call attachApplication to report to AMS, so AMS knows that the process is ready
  • AMS calls bindApplication to the Provider. BindApplication has two important jobs: one is to create the Application object, and the other is to initialize the Provider. Create the Provider object, call the lifecycle)
  • The Provider then calls publishContentProvider to publish the Binder object to AMS
  • Now that AMS has the Provider’s binder object, it returns the binder object to application A
  • Then the application can initiate a call to the Provider to add, delete, modify, and review

Let’s discuss the situation where the Provider process has been started

  • Application A invokes the add, delete, modify, and query function to the Provider
  • Application A requests the Provider’s Binder object to AMS
  • If a binder object is not present in AMS, AMS calls schedulelnstallProvider to request a binder object from the Provider process
  • The Provider then installs and initializes the Provider, invokes the life cycle, and finally publishes the Binder object to AMS through the publishContentProvider
  • Now that AMS has the Provider’s binder object, it returns the binder object to application A
  • Then the application can initiate a call to the Provider to add, delete, modify, and review