往Android系統(tǒng)中添加服務的方法教程
前言
最近因為公司的平臺要從Android 4.4.4 轉戰(zhàn) Android 6.0, 帶來的問題是之前我們在系統(tǒng)中添加了一些服務, 于是要將一些系統(tǒng)級的服務遷移過去,以及一些Framework 的自定義包.
碰巧在Gerrit上看到了添加系統(tǒng)服務這一塊的patch.正好做個總結.雖然我不是Framework工程師, 但是了解Android系統(tǒng)還是很有好處的.
如何獲取系統(tǒng)服務
我們獲取系統(tǒng)服務都是在context中,getSystemService獲取到的. 那么我們看一下getSystemService發(fā)生了哪些些事情.
getSystemService的實現(xiàn)是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>();
//這是注冊服務的方法,請注意是靜態(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)代碼塊.全是在注冊服務,并且全是我們常用的系統(tǒng)服務.
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. 我們找到了注冊系統(tǒng)服務的地方, 這里我們只需要把我們自己想注冊的服務添加進去,完成new ServiceFetcher() 的抽象方法就行啦. 這樣我們以后再getSystemService,傳入注冊時的名稱,就可以獲取到我們的服務對象了了.當然,這是4.4的方法.
Android 6.0 (Marshmallow)
我們來看一下ContextImpl的代碼
@Override
public Object getSystemService(String name) {
return SystemServiceRegistry.getSystemService(this, name);
}
我們發(fā)現(xiàn),與 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 的系統(tǒng)服務應該在SystemServiceRegistry類中添加.一樣的方式. 之后我們再getSystemService,傳入注冊時的名稱,就可以獲取到我們的服務對象了了.
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
Android 網(wǎng)絡狀態(tài)實時監(jiān)聽代碼實例(一)
本文給大家介紹Android 網(wǎng)絡狀態(tài)實時監(jiān)聽代碼實例(一),對android網(wǎng)絡狀態(tài)監(jiān)聽相關知識感興趣的朋友一起學習吧2016-03-03
Android App開發(fā)中創(chuàng)建Fragment組件的教程
這篇文章主要介紹了Android App開發(fā)中創(chuàng)建Fragment的教程,Fragment是用以更靈活地構建多屏幕界面的可UI組件,需要的朋友可以參考下2016-05-05
Android仿新浪微博oauth2.0授權界面實現(xiàn)代碼(2)
這篇文章主要為大家詳細介紹了Android仿新浪微博oauth2.0授權界面實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Kotlin實現(xiàn)網(wǎng)絡圖片下載和保存功能
根據(jù)Android多線程和網(wǎng)絡編程的知識講解和案例使用,使用Handler消息機制實現(xiàn)網(wǎng)絡圖片下載,并且保存到模擬器中,強化對Android多線程編程、網(wǎng)絡編程和文件讀寫的理解,這篇文章主要介紹了Kotlin實現(xiàn)網(wǎng)絡圖片下載和保存功能,需要的朋友可以參考下2023-02-02

