Passing Complex Types to Services

Passing complex types to and from services requires more work than passing Java primitive types. Before embarking on this work, you should get an idea of AIDL’s support for nonprimitive types:
1. AIDL supports String and CharSequence.
2. AIDL allows you to pass other AIDL interfaces, but you need to have
an import statement for each AIDL interface you reference (even if the
referenced AIDL interface is in the same package).
3. AIDL allows you to pass complex types that implement the
android.os.Parcelable interface. You need to have an import
statement in your AIDL file for these types.
4. AIDL supports java.util.List and java.util.Map, with a few
restrictions. The allowable data types for the items in the collection
include Java primitive, String, CharSequence, or
android.os.Parcelable. You do not need import statements for List
or Map, but you do need them for the Parcelables.
5. Nonprimitive types, other than String, require a directional indicator. Directional indicators include in, out, and inout. in means the value is set by the client, out means the value is set by the service, and inout means both the client and service set the value.
The Parcelable interface tells the Android runtime how to serialize and deserialize objects during the marshalling and unmarshalling process.
与services交换复杂数据比传递基本类型需要更大的工作量。在开始这个工作之前,我们需要熟悉一下AIDL所支持的非java基本类型:
1,String和CharSequence
2,AIDL interfaces(需要有个你引入的interface的import,即使在一个包中)
3,实现了android.os.Parcelable接口的复杂类型。需要import
4,java.util.List和java.util.Map,但稍有些限制。集合中的类型只能是java基本类型,String,CharSequence,或者是android.os.Parcelable。可以不用import List或者Map, 但一定要有Parcelables的import。
5,非基本类型,不同于String,需要directional indicator(方向的指示)。方向的指示包括in,out and inout。 in标示client设置value, out标示service设置value, inout标示client和service都设置value。
Parcelable interface 告诉android运行时如何序列化和反序列化对象在数据封送处理和数据分解过程中。


To properly implement an object for interprocess communication, we have to do the following:
1. Implement the Parcelable interface. This means that you implement
writeToParcel() and readFromParcel(). The write method will write
the object to the parcel and the read method will read the object from
the parcel. Note that the order in which you write properties must be
the same as the order in which you read them.
2. Add a static final property to the class with the name CREATOR. The
property needs to implement the android.os.Parcelable.Creator<T>
interface.
3. Provide a constructor for the Parcelable that knows how to create the object from the Parcel.
4. Define a Parcelable class in an .aidl file that matches the .java file containing the complex type. The AIDL compiler will look for this file when compiling your AIDL files.
为了实现跨进程的通信,我们需要做如下的工作:
1,实现parcelable接口,wirteToParcel()和readFromParcel()方法。需要注意的是写入属性和读取必须是同一个次序。
2,添加一个名称为CREATOR的静态常量,他的属性为android.os.Parcelable.Creator<T> interface.
3,提供一个构造函数
4,定义一个Parcelable的类在.aidl文件中。

待续。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值