这个类在静态代码块中注册了很多服务
static {
......
registerService(Context.ACTIVITY_SERVICE, ActivityManager.class,
new CachedServiceFetcher<ActivityManager>() {// 注册AMS
@Override
public ActivityManager createService(ContextImpl ctx) {
return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
}});
......
}
private static <T> void registerService(String serviceName, Class<T> serviceClass,
ServiceFetcher<T> serviceFetcher) {
SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
}
这里是把服务放在map里面。
registerService 字面名字是注册服务,其实他是把系统所有注册的服务的代理一次性全部取出来,用代理的时候直接从 map里面取,不用每次都在ServiceManager里面拿。SystemServiceRegistry其实作用就是缓存了所有服务的代理。
registerService 这个名字起得太烂了,cacheService应该叫这个名字合适。