怎么在Android系統(tǒng)中添加服務(wù)?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

如何獲取系統(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的源碼過會(huì)兒看.
//這是我們獲取服務(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>();
//這是注冊(cè)服務(wù)的方法,請(qǐng)注意是靜態(tài)方法
private static void registerService(String serviceName, ServiceFetcher fetcher) {
if (!(fetcher instanceof StaticServiceFetcher)) {
fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
}
SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
}
網(wǎng)站名稱:怎么在Android系統(tǒng)中添加服務(wù)-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://www.chinadenli.net/article26/deegjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、ChatGPT、網(wǎng)頁設(shè)計(jì)公司、Google、品牌網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容