博客
关于我
05.Binder系统:第8课第4节_Binder系统_分层
阅读量:726 次
发布时间:2019-03-21

本文共 934 字,大约阅读时间需要 3 分钟。

Binder系统在Java中的实现机制

Binder是一个用于进程间通信的机制,广泛应用于Android开发,特别是在多组件协作的应用场景中发挥重要作用。理解Binder的实现机制对于开发高效的Android应用程序至关重要。接下来,我将深入分析Binder系统的运行流程和实现机制。

Binder系统运行流程概述

Binder系统主要涉及三个主要组件:

  • Server(服务提供者):负责通过addService方法添加新服务,并生成对应的Binder节点。每个服务的Binder节点包含ptr和cookie等成员,即后续用以区分并识别服务。

  • ServiceManager(服务管理器):在添加服务后,会创建Binder_ref,其中包含node(指向Binder节点)和desc(描述符)。

  • Client(客户端):通过ServiceManager获取服务,并调用对应的方法。具体实现通常通过RPC层桥接到Server。

  • binder系统的类别划分

    Binder系统可以分为以下三个部分:

  • 服务层:定义接口和实现具体的服务逻辑,客户端不必关心内部实现。

  • RPC层(远程接口协议层):负责将客户端的方法调用转换为跨进程操作。

  • IPC层(进程间通信协议层):实现实际的进程间通信机制。

  • 实现细节

    RPC层实现

    RPC层的核心文件是BpGoodbyeService.cpp,定义了具体的函数实现。通过remote()方法构造数据包并调用transact函数发送数据,导致Server中的onTransact被调用,进而根据调用代码和数据解析后执行特定逻辑。

    Java实现

    在Java中,TestClient.java展示了如何通过获取服务并调用对应的方法。实际绑定过程由Binder驱动执行,客户端获取Binder实例后,通过Stub类获取服务接口并调用方法。服务接口定义在HelloService.java中,其实现方法通过RPC层调用Server的对应逻辑。

    结论

    通过对Binder系统的学习,理解了其分层架构和实现机制。这一机制简化了进程间通信,使开发者能够专注于业务逻辑,而无需深入处理底层协议。理解Binder对于构建高效、模块化的Android应用至关重要。

    转载地址:http://pligz.baihongyu.com/

    你可能感兴趣的文章
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>