天天看點

10.Binder進階:系統服務中的Binder

10.1 Binder與SystemService      在我們編寫APP程式的時候, 經常會是用getSystemService(  String serviceName ) 這個方法,來擷取一個系統的服務對象。我們檢視源碼:     frameworks/base/core/java/android/app下ContextImpl.java ,可以看到SystemService可以通過在WallpaperManager中獲得到的,而WallpaperManager的initGlobals的構造函數中,則是用ServiceManager.getService( Context.WALLPAPAER_SERVICE) 擷取到一個binder的對象。而看其他的源碼,比如InputMethodManager他是通過單例模式擷取的,如下:   

registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    return InputMethodManager.getInstance(ctx);
                }});
           

    而在InputMethodManager.java的getInstance方法中,我們可以看到如下:

/**
     * Internally, the input method manager can't be context-dependent, so
     * we have this here for the places that need it.
     * @hide
     */
    static public InputMethodManager getInstance(Looper mainLooper) {
        synchronized (mInstanceSync) {
            if (mInstance != null) {
                return mInstance;
            }
            IBinder b = ServiceManager.getService(Context.INPUT_METHOD_SERVICE);
            IInputMethodManager service = IInputMethodManager.Stub.asInterface(b);
            mInstance = new InputMethodManager(service, mainLooper);
        }
        return mInstance;
    }
           

可以看到,擷取一個InputMethodManager對象,要先通過ServiceManager擷取一個InputMethod Service的Binder對象b,然後在把這個Binder對象作為IInputManager.Stub.asInterface()的參數,傳回一個InputMethodManager的統一接口,ServiceManager(android.os包下)的getService代碼如下:

/**
     * Returns a reference to a service with the given name.
     * 
     * @param name the name of the service to get
     * @return a reference to the service, or <code>null</code> if the service doesn't exist
     */
    public static IBinder getService(String name) {
        try {
            IBinder service = sCache.get(name);
            if (service != null) {
                return service;
            } else {
                return getIServiceManager().getService(name);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "error in getService", e);
        }
        return null;
    }
           

   是以說,上面所做的就是先看我們需要的SystemService有沒有在緩存中,沒有的話,就擷取到ServiceManager再擷取。 其中getIServiceManager的代碼如下:

private static IServiceManager getIServiceManager() {
        if (sServiceManager != null) {
            return sServiceManager;
        }

        // Find the service manager
        sServiceManager = ServiceManagerNative.asInterface(BinderInternal.getContextObject());
        return sServiceManager;
    }
           

上面的BinderInternal.getContextObject()方法,傳回的是ServiceManager的全局服務代理對象,也就是一個BpBinder對象,方法是原生的方法,具體可以檢視android_util_binder.cpp檔案。服務會在Framework啟動的時候,建立并且添加的。這個在這邊就先不詳細介紹了。

10.2 詳述Manager          Manager管理的就是服務,并且一個Manager往往管理一個服務,而且Manager更多的時候,調用服務本身的API接口,由于服務都是其他程序,是以,Manager内部使用的是Binder來實作遠端調用,Manager隐藏了服務的實作細節,是以用戶端并不直接通過調用Binder來通路這些服務,而是調用Manager的方法,是以,我們可以說,Manager将服務的操作、實作細節都封裝了起來。這樣Manager就相當于用戶端通往SystemService的中間件,并使得我們可以靈活、可控地定制API。    比如說AMS,我們一般不希望使用者直接通路AMS,而是通過ActivityManager來通路,而ActivityManager内部提供了一些更具有操作性的資料結構,比如RecentTaskInfo資料類封裝了最近通路的Task清單,而MemoryInfo資料類封裝了和記憶體相關的資訊。     下面是Manager通路遠端服務的模型圖:

10.Binder進階:系統服務中的Binder