Dev Guide_01_AIDL/IPC机制

本文详细介绍了如何使用 Android Interface Definition Language (AIDL) 来实现跨进程通信 (IPC)。通过 AIDL,可以在不同 Android 进程中的组件如 Activity 和 Service 之间传递对象,实现高效的数据交换。

Designing a Remote Interface Using AIDL

In this document

  1. Implementing IPC Using AIDL
    1. Create an .aidl File
    2. Implementing the Interface
    3. Exposing Your Interface to Clients
    4. Pass by value Parameters using Parcelables
  2. Calling an IPC Method

Since each application runs in its own process, and you can write a service thatruns in a different process from your Application's UI, sometimes you need to pass objectsbetween processes. On the Android platform, one process can not normally access the memoryof another process. So to talk, they need to decompose their objects into primitives thatthe operating system can understand, and "marshall" the object across that boundary for you..

 

个人理解: 通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过Application's UI的方式写个运行在一个不同的进程中的service。注意:service运行在主线程当中,除非你自己在开启一个线程来运行service。
android平台中,一个进程通常不能访问其他进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式/基本单元,以便伪装成对象跨越边界访问。

 

The code to do that marshalling is tedious to write, so we provide the AIDL tool to do itfor you.

 

个人理解: 编写这种伪装代码相当的枯燥乏味,所以android为我们提供了AIDL工具。


AIDL (Android Interface Definition Language) is an IDLlanguage used to generate code that enables two processes on an Android-powered deviceto talk using interprocess communication (IPC). If you have codein one process (for example, in an Activity) that needs to call methods on anobject in another process (for example, a Service), you would use AIDL togenerate code to marshall the parameters.

 

个人理解:AIDL是一种接口定义语言,他可以使能在Android-powered device(android设备)上运行的两个进程用IPC去通信。如果你需要在一个进程中(例如:在一个Activity中)访问另一个进程中(例如:一个Service)某个对象的方法,你就可以使用AIDL来生成这样的代码来伪装传递各种参数。


The AIDL IPC mechanism is interface-based, similar to COM or Corba, but lighter weight. It uses a proxy class to pass values between the client and the implementation.

 

个人理解:AIDL IPC机制是基于接口的,类似于 COM or Corba,但是比这两者还要轻量级。它使用代理类在客户端之间传递、实现值。

 

重要说明:


1. 本人不才,欢迎批评指正!关于这篇文档的相关内容,可以参阅文档,很简单的。仁者见仁智者见智吧,看英文需要耐心..祝您开发顺利。


2. android的Service不是一个单独的进程它和主线程在同一个进程,也不是应用程序开启(spawn)的一个新线程它是主线程的一部分。它运行在主线程当中,如果要在服务中操作一些比较耗时的工作,可以考虑开启线程!





`/bin/sh: line 0: [: =: unary operator expected` 这个错误通常是由于 `[` 测试命令的参数问题导致的。在这个错误中,shell 期望 `[` 后面跟着一个一元运算符,但却遇到了 `=`,这表明在比较操作中可能有一个参数为空。 在提供的脚本中,问题可能出在 `$(sed -n '/vendor.xiaomi.hardware.cld_aidl-V1/'p 'vendor/sprd/treble/interface/aidl/current.txt' | cut -d " " -f 1)` 或者 `$(tail -1 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash')` 这两个命令替换的结果为空。 以下是解决此问题的步骤和修改后的脚本: ### 解决方案 1. **检查命令替换结果**:在比较之前,先检查 `sed` 和 `tail` 命令的输出是否为空。 2. **使用双引号**:在比较字符串时,使用双引号将命令替换结果括起来,以防止空字符串导致的问题。 ### 修改后的脚本 ```bash # 保存 sed 命令的输出 sed_output=$(sed -n '/vendor.xiaomi.hardware.cld_aidl-V1/'p 'vendor/sprd/treble/interface/aidl/current.txt' | cut -d " " -f 1) # 保存 tail 命令的输出 tail_output=$(tail -1 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash') # 检查输出是否为空 if [ -z "$sed_output" ] || [ -z "$tail_output" ]; then echo "One of the commands produced an empty output." exit 1 fi # 进行比较 if [ "$sed_output" = "$tail_output" ]; then touch out_vendor/soong/.intermediates/vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/vendor.xiaomi.hardware.cld_aidl-api/checkhash_1_sprd.timestamp else exit 1 fi ``` ### 解释 1. **保存命令输出**:将 `sed` 和 `tail` 命令的输出分别保存到变量 `sed_output` 和 `tail_output` 中。 2. **检查输出是否为空**:使用 `-z` 选项检查变量是否为空。如果其中一个为空,则输出错误信息并退出脚本。 3. **进行比较**:使用双引号将变量括起来进行比较,避免空字符串导致的错误。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值