
前言
最近因为公司的平台要从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系统中添加服务的方法教程全部内容,希望文章能够帮你解决往Android系统中添加服务的方法教程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)