前言
最近因為公司的平臺要從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,傳入注冊時的名稱,就可以獲取到我們的服務對象了了.
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答