系统服务的启动和注册

时间:2022-05-21 15:43:33
service list | grep context
contexthub: [android.hardware.location.IContextHubService]

这里系统服务指继承于SystemService的service.这里服务没有显示调用ServiceManager.addService

而是通过publishBinderService。


frameworks/base/services/java/com/android/server/SystemServer.java
traceBeginAndSlog("StartContextHubSystemService");
mSystemServiceManager.startService(ContextHubSystemService.class);
traceEnd();


这里先不关心注册的事,先看下mSystemServiceManager.startService
frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
    /**
     * Creates and starts a system service. The class must be a subclass of
     * {@link com.android.server.SystemService}.
     *
     * @param serviceClass A Java class that implements the SystemService interface.
     * @return The service instance, never null.
     * @throws RuntimeException if the service fails to start.
     */
    @SuppressWarnings("unchecked")
    public <T extends SystemService> T startService(Class<T> serviceClass) {
        try {
            final String name = serviceClass.getName();
            Slog.i(TAG, "Starting " + name);
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);


            // Create the service.
            if (!SystemService.class.isAssignableFrom(serviceClass)) {
                throw new RuntimeException("Failed to create " + name
                        + ": service must extend " + SystemService.class.getName());
            }
            final T service;
            try {
                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
                service = constructor.newInstance(mContext);
            } 
            startService(service);
            return service;
        }
    }


    public void startService(@NonNull final SystemService service) {
        // Register it.
        mServices.add(service);
        // Start it.
        try {
            service.onStart();
        }
    }
最终调用到service的onStart
系统服务都继承基类
frameworks/base/services/core/java/com/android/server/SystemService.java
/**
 * The base class for services running in the system process. Override and implement
 * the lifecycle event callback methods as needed.
 * 
 * The lifecycle of a SystemService:
 * 
 * The constructor is called and provided with the system {@link Context}
 * to initialize the system service.
 * {onStart()} is called to get the service running.  The service should
 * publish its binder interface at this point using
 * {publishBinderService(String, IBinder)}.  It may also publish additional
 * local interfaces that other services within the system server may use to access
 * privileged internal functions.


 * Then {onBootPhase(int)} is called as many times as there are boot phases
 * until {PHASE_BOOT_COMPLETED} is sent, which is the last boot phase. Each phase
 * is an opportunity to do special work, like acquiring optional service dependencies,
 * waiting to see if SafeMode is enabled, or registering with a service that gets
 * started after this one.
 */


ContextHubService看上去怪怪的,看下dropbox的:
public final class DropBoxManagerService extends SystemService {
    @Override
    public void onStart() {/service的名字是这里传入的
        publishBinderService(Context.DROPBOX_SERVICE, mStub);
    }

}

SystemSeriveManager: ServiceManager的区别

SystemSeriveManager用于管理继承于SystemService的service

SystemService通过函数基类函数 publishBinderService ->调用

ServiceManager.addService(name, service, allowIsolated);

和ServiceManager发生关联。

ServiceManager 是servicemanager的一个封装通过publict static函数调用addService

这样service list可以看到service.