Android中Binder機(jī)制的介紹和使用

本篇內(nèi)容介紹了“Android中Binder機(jī)制的介紹和使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

松溪ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書(shū)合作)期待與您的合作!

binder用于android進(jìn)程間的通訊。客戶(hù)端程序和系統(tǒng)服務(wù),客戶(hù)端進(jìn)程之間,都是通過(guò)binder進(jìn)行進(jìn)程間通訊的。

1.客戶(hù)端程序和系統(tǒng)服務(wù)

客戶(hù)端和系統(tǒng)服務(wù)通訊的入口:cotext.getSystemService(String name)

android.app.ContextImpl:

 
  1.  

    @Override      

  2.  

    public Object getSystemService(String name) {      

  3.  

    return SystemServiceRegistry.getSystemService(this, name);      

  4.  

    }      

android.app.SystemServiceRegistry:

靜態(tài)代碼塊注冊(cè)系統(tǒng)服務(wù):

 
  1.  

    static {      

  2.  

    registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,      

  3.  

    new CachedServiceFetcher<AccessibilityManager>() {      

  4.  

    @Override      

  5.  

    public AccessibilityManager createService(ContextImpl ctx) {      

  6.  

    return AccessibilityManager.getInstance(ctx);      

  7.  

    }});      

  8.  

     

  9.  

    registerService(Context.CAPTIONING_SERVICE, CaptioningManager.class,      

  10.  

    new CachedServiceFetcher<CaptioningManager>() {      

  11.  

    @Override      

  12.  

    public CaptioningManager createService(ContextImpl ctx) {      

  13.  

    return new CaptioningManager(ctx);      

  14.  

    }});      

  15.  

     

  16.  

    registerService(Context.ACCOUNT_SERVICE, AccountManager.class,      

  17.  

    new CachedServiceFetcher<AccountManager>() {      

  18.  

    @Override      

  19.  

    public AccountManager createService(ContextImpl ctx) {      

  20.  

    IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE);      

  21.  

    IAccountManager service = IAccountManager.Stub.asInterface(b);      

  22.  

    return new AccountManager(ctx, service);      

  23.  

    }});      

  24.  

    ....      

ServiceManager、ServiceManagerNative:

serviceManager是管理系統(tǒng)服務(wù)的一個(gè)工具類(lèi)。

 
  1.  

    sServiceManager = ServiceManagerNative      

  2.  

    .asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));      

getService方法:利用ServiceManagerProxy類(lèi)通過(guò)進(jìn)程間通訊的方式,獲取其他服務(wù)在binder驅(qū)動(dòng)中的binder對(duì)象mRemote。

xxx.Stub.asInterface(binder):

利用getService獲取到的binder對(duì)象實(shí)例化相應(yīng)服務(wù)的Proxy對(duì)象,返回給客戶(hù)端,供客戶(hù)端使用。

返回系統(tǒng)服務(wù):

 
  1.  

    /**      

  2.  

    * Gets a system service from a given context.      

  3.  

    */      

  4.  

    public static Object getSystemService(ContextImpl ctx, String name) {      

  5.  

    ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);      

  6.  

    return fetcher != null ? fetcher.getService(ctx) : null;      

  7.  

    }      

2.客戶(hù)端之間

B應(yīng)用進(jìn)程調(diào)用A應(yīng)用進(jìn)程

B bindservice 向AmS請(qǐng)求啟動(dòng)B應(yīng)用的service。啟動(dòng)service成功后會(huì)像AmS返回一個(gè)(binder驅(qū)動(dòng)中的mRemote)binder,AmS會(huì)以該binder為參數(shù)調(diào)用ActivityThread類(lèi)中的ApplicatonThread對(duì)象。接著會(huì)在ApplicationThread中回調(diào)conn接口。最后,B進(jìn)程可以利用該binder調(diào)用A應(yīng)用提供的功能。

“Android中Binder機(jī)制的介紹和使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)站欄目:Android中Binder機(jī)制的介紹和使用
地址分享:http://muchs.cn/article10/pdhedo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、App設(shè)計(jì)、定制開(kāi)發(fā)、網(wǎng)站制作網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)