资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中Binder机制的介绍和使用

本篇内容介绍了“Android中Binder机制的介绍和使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

松溪ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

binder用于android进程间的通讯。客户端程序和系统服务,客户端进程之间,都是通过binder进行进程间通讯的。

1.客户端程序和系统服务

客户端和系统服务通讯的入口: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:

静态代码块注册系统服务:

 
  1.  

    static {      

  2.  

    registerService(Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class,      

  3.  

    new CachedServiceFetcher() {      

  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() {      

  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() {      

  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是管理系统服务的一个工具类。

 
  1.  

    sServiceManager = ServiceManagerNative      

  2.  

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

getService方法:利用ServiceManagerProxy类通过进程间通讯的方式,获取其他服务在binder驱动中的binder对象mRemote。

xxx.Stub.asInterface(binder):

利用getService获取到的binder对象实例化相应服务的Proxy对象,返回给客户端,供客户端使用。

返回系统服务:

 
  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.客户端之间

B应用进程调用A应用进程

B bindservice 向AmS请求启动B应用的service。启动service成功后会像AmS返回一个(binder驱动中的mRemote)binder,AmS会以该binder为参数调用ActivityThread类中的ApplicatonThread对象。接着会在ApplicationThread中回调conn接口。最后,B进程可以利用该binder调用A应用提供的功能。

“Android中Binder机制的介绍和使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


文章名称:Android中Binder机制的介绍和使用
转载来源:http://cdkjz.cn/article/pdhedo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220