getSystemService

getSystemService最终是在SystemServiceRegistry.java里做的:

 public static Object getSystemService(ContextImpl ctx, String name) {
    ServiceFetcher fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
    return fetcher != null ? fetcher.getService(ctx) : null;
}

而SYSTEM_SERVICE_FETCHERS是registerService的时候设的:

 static  void registerService(String serviceName, Class serviceClass,
        ServiceFetcher serviceFetcher) {
    SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
    SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
}     

registerService是在ContextImplInjector.java里调用的:

 static void registerMiuiServices() {
    SystemServiceRegistry.registerService(Context.SECURITY_SERVICE, SecurityManager.class,
        new CachedServiceFetcher() {
            @Override
            public SecurityManager createService(ContextImpl ctx) {
                IBinder b = ServiceManager.getService(Context.SECURITY_SERVICE);
                ISecurityManager service = ISecurityManager.Stub.asInterface(b);
                return new SecurityManager(service);
            }
        }
    );
    SystemServiceRegistry.registerService(Context.LOCATION_POLICY_SERVICE, LocationPolicyManager.class,
        new CachedServiceFetcher() {
            @Override
            public LocationPolicyManager createService(ContextImpl ctx) {
                return new LocationPolicyManager(ILocationPolicyManager.Stub.asInterface(
                        ServiceManager.getService(Context.LOCATION_POLICY_SERVICE)));
            }
        }
    );
}     

over, 后面再加binder相关

你可能感兴趣的:(getSystemService)