往Android系统中添加服务的方法教程

前言

最近因为公司的平台要从Android 4.4.4 转战 Android 6.0, 带来的问题是之前我们在系统中添加了一些服务, 于是要将一些系统级的服务迁移过去,以及一些Framework 的自定义包.

碰巧在Gerrit上看到了添加系统服务这一块的patch.正好做个总结.虽然我不是Framework工程师, 但是了解Android系统还是很有好处的.

如何获取系统服务

我们获取系统服务都是在context中,getSystemService获取到的. 那么我们看一下getSystemService发生了哪些些事情.

getSystemService的实现是ContextImpl,我们去看一下ContextImpl的源码就知道了.

Android 4.4.4 (KitKat)

这里是Android4.4.4的源码, 6.0的源码过会儿看.

 //这是我们获取服务的路口 @Override public Object getSystemService(String name) {  //可以看到我们是从一个HashMap中拿的服务.  ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);  return fetcher == null ? null : fetcher.getService(this); } private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =    new HashMap<String, ServiceFetcher>(); //这是注册服务的方法,请注意是静态方法 private static void registerService(String serviceName, ServiceFetcher fetcher) {  if (!(fetcher instanceof StaticServiceFetcher)) {   fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;  }  SYSTEM_SERVICE_MAP.put(serviceName, fetcher); } 

我们还在ContextImpl中看到很多静态代码块.全是在注册服务,并且全是我们常用的系统服务.

 static {   registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {     public Object getService(ContextImpl ctx) {      return AccessibilityManager.getInstance(ctx);     }});   registerService(CAPTIONING_SERVICE, new ServiceFetcher() {     public Object getService(ContextImpl ctx) {      return new CaptioningManager(ctx);     }});  .... }

这么看来,这不就是我们注册服务的地方么?

So. 我们找到了注册系统服务的地方, 这里我们只需要把我们自己想注册的服务添加进去,完成new ServiceFetcher() 的抽象方法就行啦. 这样我们以后再getSystemService,传入注册时的名称,就可以获取到我们的服务对象了了.当然,这是4.4的方法.

Android 6.0 (Marshmallow)

我们来看一下ContextImpl的代码

 @Override public Object getSystemService(String name) {  return SystemServiceRegistry.getSystemService(this, name); }

我们发现,与 KitKat 大大不同, Marshmallow这里是从一个叫做SystemServiceRegistry的类去获取的.

好了,那我们去看它的源码,原来还是和以前一样的套路,不过是单独封装了一个类来管理这些注册的服务. 这么设计的确好,代码上的耦合度看上去小多了,且不会使得ContextImpl这个类越来月臃肿.

final class SystemServiceRegistry { private final static String TAG = "SystemServiceRegistry"; // Service registry information. // This information is never changed once static initialization has completed. private static final HashMap<Class<?>, String> SYSTEM_SERVICE_NAMES =   new HashMap<Class<?>, String>(); private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =   new HashMap<String, ServiceFetcher<?>>(); private static int sServiceCacheSize; // Not instantiable. private SystemServiceRegistry() { } static {  registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,    new CachedServiceFetcher<AccessibilityManager>() {   @Override   public AccessibilityManager createService(ContextImpl ctx) {    return AccessibilityManager.getInstance(ctx);   }});  registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,    new CachedServiceFetcher<CaptioningManager>() {   @Override   public CaptioningManager createService(ContextImpl ctx) {    return new CaptioningManager(ctx);   }}); ....

So.我们 Marshmallow 的系统服务应该在SystemServiceRegistry类中添加.一样的方式. 之后我们再getSystemService,传入注册时的名称,就可以获取到我们的服务对象了了.

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

莫找借口失败,只找理由成功。

往Android系统中添加服务的方法教程

相关文章:

你感兴趣的文章:

标签云: