The difference between boot modes

System services are started

Most of the system services run in the System Server, but also in it to start, in the system Server startup by the way to start services such as AMS,WMS,PMS are in the System Server

private void run() {... startBootstrapService(); startCoreService(); startOtherServices(); . }Copy the code
  • Starting services does not necessarily mean starting worker threads. Most services run with binder threads, and a few have their own worker threads. So what does it mean to start a service? It mainly does init work for the service, such as preparing the binder entity object for the service. When the client has a request, it distributes the request to the corresponding binder entity object in the Binder thread pool for processing, and then replies to the client

  • There are also some services that are not in the System Server, but have a process of their own. These services are usually native implementations and have their own main entry function. They need to enable binder mechanism and manage binder communication, which is more complicated, but they also need to have binder entities. Binder thread. Binder thread waits for client request and then distributes to binder entity object.

The application service is started

Both start service and bind service requests are sent from the application to AMS.

ComponentName startServiceCommon(...) {... // Get the AMS binder object. The startSetvice is an IPC call that creates ServiceRecord. // The ServiceRecord is only a record of the service, and AMS is only responsible for the management and dispatch of the service. Start and load or want in the application of the service side do ActivityManagerNative. GetDefault () startService (...). ; }Copy the code

How does an application start and load a service

Private void handleCreateService(CreateServiceData data) { NewInstance creates an object for service service = (service)cl.loadClass(data.info.name).neWinstance (); / / to the service create context ContextImpl context. = ContextImpl createAppContext (this) that... ; //create application Application app = packageInfo.makeApplication(false,...). ; //attach service service.attach(context, this, ...) ; // Execute the declaration cycle callback service.oncreate (); }Copy the code

Differences in registration methods

Registration of system services

// In the System Server process, the Java layer implements pubic voidsetSystemProcess(){
    ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true); . Int main(int, char**){sp<IServiceManager> sm(defaultServiceManager()); sm->addService(String16(SurfaceFlinger::getServiceName(), flinger,false));
}
Copy the code
  1. Either in the SysemServer process or as a stand-alone process, register services with the Service Manager.

  2. Binder entity objects registered with service Manager must prompt permission error, because only system services can be registered with Service Mananger.

Registering an application service

  1. The application makes a binderService call to AMS,
  2. AMS looks to see if the service is registered and returns the Binder’s service object directly to the application.
  3. If no, the AMS requests the Binder object, and the Service responds to the request by registering its own binder object with AMS. AMS then calls the binder object back to the application.

Use the difference

Use of system services

PowerManager PM = context.getSystemService(context.power_service); PowerManager PM = context.getSystemService(); // Call the object's interface function using the system service PowerManagfer.wakelock = pm.newWakelock(flags.tag);Copy the code

SystemServiceRegistry.java

RegisterService (context. POWER_SERVICE, powermanager. class, serviceFetcher) New CachedServiceFetcher<PowerManager>(){@override public PowerManager createService(ContextImpl) CTX) {/ / by service manager getService function first acquisition system service the IBinder binder object b = ServiceManager. GetService (Context. POWER_SERVICE); / / object encapsulates a service layer of management object to the AP, convenient application layer calls IPowerManager service = IPowerManager. The Stub. AsInterface (b);return new PowerManager(ctx.getOuterContext(),..);
        }
    });
Copy the code

Use of application services

bindService(serviceIntent, new ServiceConection(){
    @Override
    //AMS returns a service IBinder object to the AP side with an onServiceConnected call back.
    public void onServiceConnected(ComponentName name, IBinder service){
        // Bind the binder object service to a layer of business interface objects, and then hold the object to make calls to AP servicesIMyInterface myInterface = IMyInterface.Stub.asInterface(service); }})Copy the code

reference

  • Profiling Framework interview hits top Android positions
  • Android getSystemService