目录
前言
记录一下getSystemService()获取的源码流程,加深一下印象。
正文
这里以获取AudioManager为例
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
getSystemService()定义在Context.java中
public static final String AUDIO_SERVICE = "audio";
Context.java
frameworks\base\core\java\android\content\Context.java
public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name);
抽象方法。
Context的实现类是ContextImpl,具体看ContextImpl.java
ContextImpl.java
\frameworks\base\core\java\android\app\ContextImpl.java
@Override public Object getSystemService(String name) { if (vmIncorrectContextUseEnabled()) { //略 } //重点关注后面的 return SystemServiceRegistry.getSystemService(this, name); }
SystemServiceRegistry.java
\frameworks\base\core\java\android\app\SystemServiceRegistry.java
我们一开始传入的name是Context.AUDIO_SERVICE,也就是audio。
public static Object getSystemService(ContextImpl ctx, String name) { //不为null if (name == null) { return null; } //SYSTEM_SERVICE_FETCHERS是map,存储了服务相关信息 final ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name); if (fetcher == null) { if (sEnableServiceNotFoundWtf) { Slog.wtf(TAG, "Unknown manager requested: " + name); } return null; } //获取服务,这里需要重点关注fetcher final Object ret = fetcher.getService(ctx); if (sEnableServiceNotFoundWtf && ret == null) { // Some services do return null in certain situations, so don't do WTF for them. switch (name) { case Context.CONTENT_CAPTURE_MANAGER_SERVICE: case Context.APP_PREDICTION_SERVICE: case Context.INCREMENTAL_SERVICE: case Context.ETHERNET_SERVICE: return null; } return null; } return ret; }
上面重点关注的是SYSTEM_SERVICE_FETCHERS和ServiceFetcher。
SYSTEM_SERVICE_FETCHERS
private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS = new ArrayMap<String, ServiceFetcher<?>>();
具体看哪里添加的
registerService
//私有的静态变量,类内部使用 private static <T> void registerService(@NonNull String serviceName, @NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) { SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName); //这里添加的 SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher); SYSTEM_SERVICE_CLASS_NAMES.put(serviceName, serviceClass.getSimpleName()); }
这里有多个map存储服务相关信息,我们这里值关注SYSTEM_SERVICE_FETCHERS。
第一个是服务名,第二个是serviceFetcher。
在SystemServiceRegistry中有一个静态代码块,这里注册了
Context.ACTIVITY_SERVICE Context.AUDIO_SERVICE Context.ACCOUNT_SERVICE Context.ACCESSIBILITY_SERVICE Context.HDMI_CONTROL_SERVICE 等
这里只关注AUDIO_SERVICE的注册。
static{ //略 registerService(Context.AUDIO_SERVICE, AudioManager.class, new CachedServiceFetcher<AudioManager>() { @Override public AudioManager createService(ContextImpl ctx) { return new AudioManager(ctx); }}); //略 }
第三个是匿名创建CachedServiceFetcher
CachedServiceFetcher
static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> { private final int mCacheIndex; CachedServiceFetcher() { mCacheIndex = sServiceCacheSize++; } @Override @SuppressWarnings("unchecked") public final T getService(ContextImpl ctx) { final Object[] cache = ctx.mServiceCache; final int[] gates = ctx.mServiceInitializationStateArray; boolean interrupted = false; T ret = null; //循环,知道获取成功就退出 for (;;) { boolean doInitialize = false; synchronized (cache) { //如果存在缓存,就返回 T service = (T) cache[mCacheIndex]; if (service != null) { ret = service; break; // exit the for (;;) } if (gates[mCacheIndex] == ContextImpl.STATE_READY || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) { gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED; } if (gates[mCacheIndex] == ContextImpl.STATE_UNINITIALIZED) { doInitialize = true; gates[mCacheIndex] = ContextImpl.STATE_INITIALIZING; } } if (doInitialize) { T service = null; @ServiceInitializationState int newState = ContextImpl.STATE_NOT_FOUND; try { service = createService(ctx); newState = ContextImpl.STATE_READY; } catch (ServiceNotFoundException e) { onServiceNotFound(e); } finally { synchronized (cache) { cache[mCacheIndex] = service; gates[mCacheIndex] = newState; cache.notifyAll(); } } ret = service; break; } synchronized (cache) { while (gates[mCacheIndex] < ContextImpl.STATE_READY) { try { interrupted |= Thread.interrupted(); cache.wait(); } catch (InterruptedException e) { interrupted = true; } } } } if (interrupted) { Thread.currentThread().interrupt(); } return ret; } public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException; }
上面代码很多,但最重要的是需要重写的createService()。
因此回到上面第三个参数创建匿名CachedServiceFetcher对象代码处。
new CachedServiceFetcher<AudioManager>() { @Override public AudioManager createService(ContextImpl ctx) { return new AudioManager(ctx); }}
这里是直接返回了新创建的AudioManager对象。
至此,上面
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
就类似等价于
AudioManager audioManager = new AudioManager(ctx);
参考文章
© 版权声明