AIDL文件中使用实现接口Parcelable的类
2013-12-19 20:48
225 查看
在官方文档中说明了,在AIDL文件中可以使用:
AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。
其中对于Java编程语言的基本数据类型 (int, long, char, boolean等),String和CharSequence,集合接口类型List和Map,不需要import 语句。
而如果需要在AIDL中使用其他AIDL接口类型,需要import,即使是在相同包结构下。AIDL允许传递实现Parcelable接口的类,需要import.
需要特别注意的是,对于非基本数据类型,也不是String和CharSequence类型的,需要有方向指示,包括in、out和inout,in表示由客户端设置,out表示由服务端设置,inout是两者均可设置。
AIDL只支持接口方法,不能公开static变量。
其中就说明了可以使用:Parcelable接口的类
比如:IMyService.aidl
package com.demo;
import com.demo.Person;
interface IMyService {
void savePersonInfo(in
Person person);
List<Person> getAllPerson();
}
在这里使用了Person类,如果Person没有使用什么神奇的功效是不可使用在这里的。
看下:Person类的神奇之地:
public class Person implements Parcelable
{
private String
name;
private String
telNumber;
private int age;
public Person()
{}
public Person(Parcel pl){
name = pl.readString();
telNumber = pl.readString();
age = pl.readInt();
}
public String
getName() {
return name;
}
public void setName(String
name) {
this.name
= name;
}
public String
getTelNumber() {
return telNumber;
}
public void setTelNumber(String
telNumber) {
this.telNumber
= telNumber;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age
= age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(telNumber);
dest.writeInt(age);
}
public static final Parcelable.Creator<Person>
CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person
createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[]
newArray(int size) {
return new Person[size];
}
};
}
对于Parcelable的使用在这里简单的描述下:
是一个序列化的类,这里使用Parcelable 接口来序列化,是Android提供的一个比Serializable 效率更高的序列化类。
Parcelable需要实现三个函数:
1) void writeToParcel(Parcel dest, int flags) 将需要序列化存储的数据写入外部提供的Parcel对象dest。而看了网上的代码例子,个人猜测,读取Parcel数据的次序要和这里的write次序一致,否则可能会读错数据。具体情况我没试验过!
2) describeContents() 没搞懂有什么用,反正直接返回0也可以
3) static final Parcelable.Creator对象CREATOR 这个CREATOR命名是固定的,而它对应的接口有两个方法:
createFromParcel(Parcel source) 实现从source创建出JavaBean实例的功能。
最后,别忘了创建文件
Person.aidl
内容如下:
package com.demo;
parcelable Person;
注意这里的parcelable和原来实现的Parcelable 接口,开头的字母p一个小写一个大写。
对于实现AIDL接口,官方还提醒我们:
1. 调用者是不能保证在主线程执行的,所以从一调用的开始就需要考虑多线程处理,以及确保线程安全;
2. IPC调用是同步的。如果你知道一个IPC服务需要超过几毫秒的时间才能完成地话,你应该避免在Activity的主线程中调用。也就是IPC调用会挂起应用程序导致界面失去响应,这种情况应该考虑单独开启一个线程来处理。
3. 抛出的异常是不能返回给调用者(跨进程抛异常处理是不可取的)。
对于,其他的部分见前面的一文:Android 服务Service
<service android:name=".YourService" android:process=":remote">
<intent-filter>
<action android:name="com.demo.IMyService" />
</intent-filter>
</service>
android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。不加,可能会提示空指针异常。
AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。
其中对于Java编程语言的基本数据类型 (int, long, char, boolean等),String和CharSequence,集合接口类型List和Map,不需要import 语句。
而如果需要在AIDL中使用其他AIDL接口类型,需要import,即使是在相同包结构下。AIDL允许传递实现Parcelable接口的类,需要import.
需要特别注意的是,对于非基本数据类型,也不是String和CharSequence类型的,需要有方向指示,包括in、out和inout,in表示由客户端设置,out表示由服务端设置,inout是两者均可设置。
AIDL只支持接口方法,不能公开static变量。
其中就说明了可以使用:Parcelable接口的类
比如:IMyService.aidl
package com.demo;
import com.demo.Person;
interface IMyService {
void savePersonInfo(in
Person person);
List<Person> getAllPerson();
}
在这里使用了Person类,如果Person没有使用什么神奇的功效是不可使用在这里的。
看下:Person类的神奇之地:
public class Person implements Parcelable
{
private String
name;
private String
telNumber;
private int age;
public Person()
{}
public Person(Parcel pl){
name = pl.readString();
telNumber = pl.readString();
age = pl.readInt();
}
public String
getName() {
return name;
}
public void setName(String
name) {
this.name
= name;
}
public String
getTelNumber() {
return telNumber;
}
public void setTelNumber(String
telNumber) {
this.telNumber
= telNumber;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age
= age;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(telNumber);
dest.writeInt(age);
}
public static final Parcelable.Creator<Person>
CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person
createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[]
newArray(int size) {
return new Person[size];
}
};
}
对于Parcelable的使用在这里简单的描述下:
是一个序列化的类,这里使用Parcelable 接口来序列化,是Android提供的一个比Serializable 效率更高的序列化类。
Parcelable需要实现三个函数:
1) void writeToParcel(Parcel dest, int flags) 将需要序列化存储的数据写入外部提供的Parcel对象dest。而看了网上的代码例子,个人猜测,读取Parcel数据的次序要和这里的write次序一致,否则可能会读错数据。具体情况我没试验过!
2) describeContents() 没搞懂有什么用,反正直接返回0也可以
3) static final Parcelable.Creator对象CREATOR 这个CREATOR命名是固定的,而它对应的接口有两个方法:
createFromParcel(Parcel source) 实现从source创建出JavaBean实例的功能。
最后,别忘了创建文件
Person.aidl
内容如下:
package com.demo;
parcelable Person;
注意这里的parcelable和原来实现的Parcelable 接口,开头的字母p一个小写一个大写。
对于实现AIDL接口,官方还提醒我们:
1. 调用者是不能保证在主线程执行的,所以从一调用的开始就需要考虑多线程处理,以及确保线程安全;
2. IPC调用是同步的。如果你知道一个IPC服务需要超过几毫秒的时间才能完成地话,你应该避免在Activity的主线程中调用。也就是IPC调用会挂起应用程序导致界面失去响应,这种情况应该考虑单独开启一个线程来处理。
3. 抛出的异常是不能返回给调用者(跨进程抛异常处理是不可取的)。
对于,其他的部分见前面的一文:Android 服务Service
<service android:name=".YourService" android:process=":remote">
<intent-filter>
<action android:name="com.demo.IMyService" />
</intent-filter>
</service>
android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。不加,可能会提示空指针异常。
相关文章推荐
- 使用WinHttp接口实现HTTP协议Get、Post和文件上传功能
- Android文件存储的问题:ClassLoader和实现Parcelable接口后 详解及用途
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- Android Service 的使用(五)----如何实现AIDL文件中含有对象的调用
- linux 使用系统接口实现复制文件
- windows下VS2010中使用netcdf的C++接口实现.nc文件的读写
- 使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- 使用七牛接口实现图片和文件一键上传
- 使用Parcelable接口实现数据的序列化
- android studio 中aidl文件的使用并且实现跨进程的通信
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- 使用WinHttp接口实现HTTP协议Get、Post和文件上传功能
- 关于使用RESTful api上传文件,基于jax rs接口,不是实现
- 使用mybatis-generator自动生成实体类,接口实现类和Mapper映射配置文件
- 实现HTTP协议Get、Post和文件上传功能——使用WinHttp接口实现
- Android中使用AIDL接口实现进程间通信
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- 使用intent传递实现Serializable、Parcelable接口的类对象
- 使用leadtools的WCF接口功能实现从web端上传Dicom影像文件