往Android系統(tǒng)中添加服務(wù)的方法教程
前言
最近因?yàn)楣镜钠脚_要從Android 4.4.4 轉(zhuǎn)戰(zhàn) Android 6.0, 帶來的問題是之前我們在系統(tǒng)中添加了一些服務(wù), 于是要將一些系統(tǒng)級的服務(wù)遷移過去,以及一些Framework 的自定義包.
碰巧在Gerrit上看到了添加系統(tǒng)服務(wù)這一塊的patch.正好做個(gè)總結(jié).雖然我不是Framework工程師, 但是了解Android系統(tǒng)還是很有好處的.
如何獲取系統(tǒng)服務(wù)
我們獲取系統(tǒng)服務(wù)都是在context中,getSystemService獲取到的. 那么我們看一下getSystemService發(fā)生了哪些些事情.
getSystemService的實(shí)現(xiàn)是ContextImpl,我們?nèi)タ匆幌翪ontextImpl的源碼就知道了.
Android 4.4.4 (KitKat)
這里是Android4.4.4的源碼, 6.0的源碼過會兒看.
//這是我們獲取服務(wù)的路口 @Override public Object getSystemService(String name) { //可以看到我們是從一個(gè)HashMap中拿的服務(wù). 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>(); //這是注冊服務(wù)的方法,請注意是靜態(tài)方法 private static void registerService(String serviceName, ServiceFetcher fetcher) { if (!(fetcher instanceof StaticServiceFetcher)) { fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++; } SYSTEM_SERVICE_MAP.put(serviceName, fetcher); }
我們還在ContextImpl中看到很多靜態(tài)代碼塊.全是在注冊服務(wù),并且全是我們常用的系統(tǒng)服務(wù).
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); }}); .... }
這么看來,這不就是我們注冊服務(wù)的地方么?
So. 我們找到了注冊系統(tǒng)服務(wù)的地方, 這里我們只需要把我們自己想注冊的服務(wù)添加進(jìn)去,完成new ServiceFetcher()
的抽象方法就行啦. 這樣我們以后再getSystemService,傳入注冊時(shí)的名稱,就可以獲取到我們的服務(wù)對象了了.當(dāng)然,這是4.4的方法.
Android 6.0 (Marshmallow)
我們來看一下ContextImpl的代碼
@Override public Object getSystemService(String name) { return SystemServiceRegistry.getSystemService(this, name); }
我們發(fā)現(xiàn),與 KitKat 大大不同, Marshmallow這里是從一個(gè)叫做SystemServiceRegistry的類去獲取的.
好了,那我們?nèi)タ此脑创a,原來還是和以前一樣的套路,不過是單獨(dú)封裝了一個(gè)類來管理這些注冊的服務(wù). 這么設(shè)計(jì)的確好,代碼上的耦合度看上去小多了,且不會使得ContextImpl這個(gè)類越來月臃腫.
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 的系統(tǒng)服務(wù)應(yīng)該在SystemServiceRegistry類中添加.一樣的方式. 之后我們再getSystemService,傳入注冊時(shí)的名稱,就可以獲取到我們的服務(wù)對象了了.
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一)
本文給大家介紹Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一),對android網(wǎng)絡(luò)狀態(tài)監(jiān)聽相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03Android實(shí)現(xiàn)微信側(cè)滑關(guān)閉頁面效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信側(cè)滑關(guān)閉頁面效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android App開發(fā)中創(chuàng)建Fragment組件的教程
這篇文章主要介紹了Android App開發(fā)中創(chuàng)建Fragment的教程,Fragment是用以更靈活地構(gòu)建多屏幕界面的可UI組件,需要的朋友可以參考下2016-05-05Android仿新浪微博oauth2.0授權(quán)界面實(shí)現(xiàn)代碼(2)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博oauth2.0授權(quán)界面實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Kotlin實(shí)現(xiàn)網(wǎng)絡(luò)圖片下載和保存功能
根據(jù)Android多線程和網(wǎng)絡(luò)編程的知識講解和案例使用,使用Handler消息機(jī)制實(shí)現(xiàn)網(wǎng)絡(luò)圖片下載,并且保存到模擬器中,強(qiáng)化對Android多線程編程、網(wǎng)絡(luò)編程和文件讀寫的理解,這篇文章主要介紹了Kotlin實(shí)現(xiàn)網(wǎng)絡(luò)圖片下載和保存功能,需要的朋友可以參考下2023-02-02Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10Android實(shí)現(xiàn)獲取SERIAL信息的方法
這篇文章主要介紹了Android實(shí)現(xiàn)獲取SERIAL信息的方法,涉及Android操作SERIAL的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10