当前位置:   article > 正文

系统服务 SystemServiceRegistry

systemserviceregistry
  1. 在开发过程中,常常会用到系统服务,有时候也会添加一些系统服务;
  2. 这里看一下Android P相关源码;
  3. 在APP层获取系统服务的接口是getSystemService;
  4. 它被定义在frameworks/base/core/java/android/content/Context.java
  5. public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name);
  6. 这里的@ServiceName是一个注解,限定了输入,如果对注解内容不是很了解可以看下本人下面的文章:
  7. Java注解,小试牛刀
  8. 这个直接定义在Context.java中,之所以要特地提一下这个注解,是因为在它限定了输入内容之后,我们新加的服务如果想通过getSystemService被拿到,就得同步把名字加到这个注解里;
  9. 当然Context.java内方法的具体的实现都在frameworks/base/core/java/android/app/ContextImpl.java里面;
  10. @Override
  11. public Object getSystemService(String name) {
  12. return SystemServiceRegistry.getSystemService(this, name);
  13. }
  14. 这里引出了一个关键的类SystemServiceRegistry;
  15. 先看一下名词解释;
  16. /**
  17. * Manages all of the system services that can be returned by {@link Context#getSystemService}.
  18. * Used by {@link ContextImpl}.
  19. * @hide
  20. */
  21. public final class SystemServiceRegistry
  22. 再整体看一眼SystemServiceRegistry这个类;
  23. 位置:frameworks/base/core/java/android/app/SystemServiceRegistry.java
  24. 代码不多才1000多行;
  25. 它的构造方法是私有的,不允许被实例化;
  26. // Not instantiable.
  27. private SystemServiceRegistry() { }
  28. 主要成员变量;
  29. // Service registry information.
  30. // This information is never changed once static initialization has completed.
  31. private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =
  32. new HashMap<Class<?>, String>();
  33. private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
  34. new HashMap<String, ServiceFetcher<?>>();
  35. private static int sServiceCacheSize;
  36. 类里面会静态地把很多系统服务加到cache里面,这里分别看3种不同类型的例子,下面会有更多介绍;
  37. static {
  38. registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,
  39. new CachedServiceFetcher<AccessibilityManager>() {
  40. @Override
  41. public AccessibilityManager createService(ContextImpl ctx) {
  42. return AccessibilityManager.getInstance(ctx);
  43. }});
  44. //...
  45. registerService(Context.JOB_SCHEDULER_SERVICE, JobScheduler.class,
  46. new StaticServiceFetcher<JobScheduler>() {
  47. @Override
  48. public JobScheduler createService() throws ServiceNotFoundException {
  49. IBinder b = ServiceManager.getServiceOrThrow(Context.JOB_SCHEDULER_SERVICE);
  50. return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b));
  51. }});
  52. //...
  53. registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
  54. new StaticApplicationContextServiceFetcher<ConnectivityManager>() {
  55. @Override
  56. public ConnectivityManager createService(Context context) throws ServiceNotFoundException {
  57. IBinder b = ServiceManager.getServiceOrThrow(Context.CONNECTIVITY_SERVICE);
  58. IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
  59. return new ConnectivityManager(context, service);
  60. }});
  61. //...
  62. }
  63. 主要的API:
  64. /**
  65. * Gets a system service from a given context.
  66. */
  67. public static Object getSystemService(ContextImpl ctx, String name) {
  68. ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
  69. return fetcher != null ? fetcher.getService(ctx) : null;
  70. }
  71. /**
  72. * Gets the name of the system-level service that is represented by the specified class.
  73. */
  74. public static String getSystemServiceName(Class<?> serviceClass) {
  75. return SYSTEM_SERVICE_NAMES.get(serviceClass);
  76. }
  77. /**
  78. * Statically registers a system service with the context.
  79. * This method must be called during static initialization only.
  80. */
  81. private static <T> void registerService(String serviceName, Class<T> serviceClass,
  82. ServiceFetcher<T> serviceFetcher) {
  83. SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
  84. SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
  85. }
  86. 主要注册、获取服务、获取服务名称这三个;
  87. 这里涉及到一个称为ServiceFetcher的东西,它是注册服务和获取服务的关键;
  88. 其实它是一个接口,用来获取真正的服务;
  89. /**
  90. * Base interface for classes that fetch services.
  91. * These objects must only be created during static initialization.
  92. */
  93. public static abstract interface ServiceFetcher<T> {
  94. T getService(ContextImpl ctx);
  95. }
  96. 一共有3个抽象类实现了此接口:
  97. CachedServiceFetcher、StaticServiceFetcher、StaticApplicationContextServiceFetcher
  98. 它们的工作方式是:如果服务已经存在就直接返回,如果不存在就调用createService抽象方法创建一个;
  99. /**
  100. * Override this class when the system service constructor needs a
  101. * ContextImpl and should be cached and retained by that context.
  102. */
  103. public static abstract class CachedServiceFetcher<T> implements ServiceFetcher<T> {
  104. private final int mCacheIndex;
  105. public CachedServiceFetcher() {
  106. // Note this class must be instantiated only by the static initializer of the
  107. // outer class (SystemServiceRegistry), which already does the synchronization,
  108. // so bare access to sServiceCacheSize is okay here.
  109. mCacheIndex = sServiceCacheSize++;
  110. }
  111. @Override
  112. @SuppressWarnings("unchecked")
  113. public final T getService(ContextImpl ctx) {
  114. //...
  115. }
  116. public abstract T createService(ContextImpl ctx) throws ServiceNotFoundException;
  117. }
  118. /**
  119. * Override this class when the system service does not need a ContextImpl
  120. * and should be cached and retained process-wide.
  121. */
  122. static abstract class StaticServiceFetcher<T> implements ServiceFetcher<T> {
  123. private T mCachedInstance;
  124. @Override
  125. public final T getService(ContextImpl ctx) {
  126. synchronized (StaticServiceFetcher.this) {
  127. //...
  128. return mCachedInstance;
  129. }
  130. }
  131. public abstract T createService() throws ServiceNotFoundException;
  132. }
  133. /**
  134. * Like StaticServiceFetcher, creates only one instance of the service per application, but when
  135. * creating the service for the first time, passes it the application context of the creating
  136. * application.
  137. *
  138. * TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
  139. * case where multiple application components each have their own ConnectivityManager object.
  140. */
  141. static abstract class StaticApplicationContextServiceFetcher<T> implements ServiceFetcher<T>{
  142. private T mCachedInstance;
  143. @Override
  144. public final T getService(ContextImpl ctx) {
  145. //...
  146. return mCachedInstance;
  147. }
  148. public abstract T createService(Context applicationContext) throws ServiceNotFoundException;
  149. }
  150. 小结:
  151. SystemServiceRegistry这个东西维护了所有的服务,具体服务在哪儿还得从对应的createService里面找;

转自  系统服务 SystemServiceRegistry-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/227981
推荐阅读
相关标签
  

闽ICP备14008679号