AIDL(Android Interface Definition Language)的搭建

温故而知新可以为师矣!

AIDL(Android Interface Definition Language):是用于生成可以在Android设备上两个进程之间通信的代码的语言

写AIDL步骤:
服务器端(被调用端):
1、创建IStudentService.aidl文件
package com.example.remoteservice;
import com.example.remoteservice.Student;
/**
 * @author HuangYK
 *编写AIDL需要注意:
  *1、接口名和AIDL文件名相同
  *2、接口和方法前不用加访问权限修饰符public、protect、private等,也不能用final、static
  *3、AIDL默认支持的类型包括Java基本类型(int,long,boolean等)和string、list、map
  *   charSequence,使用这些类型是不需要import声明,对于list和map中的元素类型必须是
  *   AIDL支持的类型,如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable(可打包的)接口
  *4、自定义类型和AIDL生成的其他接口类型在AIDL描述文件中,应该显示import即使在该类和定义的包在同一包中
  *5、在AIDL文件中所有非Java 基本类型的参数必须加上in、out、标记,以表明输入参数、输出参数还是输入输出参数
  *6、java原始类型默认的标记为int,不能为其他标记
  *7、引用非基本类型的类需要导入包
 *
 */
interface IStuendentService {
    Student getStudentById(int id);
}

2、写实体bean(Student)对象:
public class Student implements Parcelable{
    private int id;
    private int age;
    private String name;
    public int getId() {
        return id;
    }
    /**
     * @param id
     * @param age
     * @param name
     */
    public Student(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    /* 
     *将当前对象的属性数据打成包,写到包对象中
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //写到包对象中
        dest.writeInt(id);
        dest.writeInt(age);
        dest.writeString(name);
    }
    //添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口
    //名字必须为REATOR
    public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {
        //返回一個指定大小的指定容器
        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
        //解包,读取包中的数据并封装成对象
        @Override
        public Student createFromParcel(Parcel source) {
            //读的顺序要和写入的数据一样,就是和dest.write的顺序一样的,因为他是一整快完整的内存去存储的
            int id = source.readInt();
            int age = source.readInt();
            String name = source.readString();
            return new Student(id, age, name);
        }
    };
需要注意:a、该对象要实现Parcelable接口,并重写writeToParcel()方法,将当前对象的属性数据打成包,写到包对象中。b、要添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口,名字必须为REATOR,并实现createFromParcel()方法,解包,读取包中的数据并封装成对象。
3、创建文件Student.aidl
package com.example.remoteservice;
parcelable Student;
注意:到这里的话系统会自动生成IStudentService 对应的java文件,在该文件里面有IStudentService里面定义的getStudentById方法可以直接继承使用
4、书写本地的MyRemeoteService
public class MyRemeoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return new StudentService();
    }
    /*
     * 处理Student相关的业务逻辑类
     * 里面进行业务的处理,比如查询数据库
     */
    class StudentService extends IStuendentService.Stub{

        @Override
        public Student getStudentById(int id) throws RemoteException {
            return new Student(id, 24, "HYK");
        }

    }
}
注意点:onBind()方法返回的是继承了IStuendentService.Stub接口并实现了getStudentById方法的子类,我需要做的事情一般在getStudentById()这个方法里面进行,比如数据库的操作或者运用程序的其他操作。
5、在manifest文件里面配置
<service android:name="com.example.remoteservice.MyRemeoteService">
            <!-- 进程间的AIDL需要进行配置intent-filter -->
            <intent-filter>
                <action android:name="com.example.remoteservice.action" />
            </intent-filter>
        </service>
这步很重要,如果不配置action的话别的应用(进程)无法调用


客户端(调用端)
1、复制服务器端(被调用端)AIDL的相关定义,即Student.java、IStudent.aidl、Student.aidl文件,并且包名也不能改
2、在activity里面bind远程Service,并调用业务方法
Intent intent = new Intent(
                    "com.example.remoteservice.action");
            if (conn == null) {

                conn = new ServiceConnection() {

                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        // TODO Auto-generated method stub
                        Log.i("TAG", "onServiceDisconnected:"+name);
                    }

                    @Override
                    public void onServiceConnected(ComponentName name,
                            IBinder service) {

                        Log.i("TAG", "onServiceConnected:"+name);
                        //
                        studentService = IStuendentService.Stub.asInterface(service);
                    }
                };
                bindService(intent, conn, Context.BIND_AUTO_CREATE);

注意:a、intent里面的“com.example.remoteservice.action”是在服务器端的manifest文件里面定义的action的名字。
            b、onServiceConnected()返回的是IStuendentService.Stub.asInterface(service);接口对象,通过这个对象就可以调用业务方法:Student student = studentService.getStudentById(id);


源码:http://download.youkuaiyun.com/detail/qq_17265737/9675964












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值