溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android系統中添加服務

發布時間:2020-12-04 16:59:53 來源:億速云 閱讀:253 作者:Leah 欄目:移動開發

怎么在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);
     }});
  ....
 }

這么看來,這不就是我們注冊服務的地方么&#63;

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<&#63;>, String> SYSTEM_SERVICE_NAMES =
   new HashMap<Class<&#63;>, String>();
 private static final HashMap<String, ServiceFetcher<&#63;>> SYSTEM_SERVICE_FETCHERS =
   new HashMap<String, ServiceFetcher<&#63;>>();
 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系統中添加服務問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女