AIDL的学习记录

参考资料:谷歌官方文档

AIDL使用场景 :一对多通信且有RPC(Remote Procedure Call Protocol远程过程调用协议)需求

支持的类型:

  • 基本数据类型:byte,int,long,char,booblean,double,float(除short)
  • String和CharSequence
  • List:只支持ArrayList,里面的元素都必须被AIDL支持
  • Map:只支持HashMap,里面的元素都必须被AIDL支持
  • Parcelable:所有实现了Parcelable接口的对象
  • AIDL:所有AIDL接口本身也可以载AIDL中使用
    Parcelable和AIDL必须要import,除基本类型外,其他类型的参数必须标上方向:in(输入)、out(输出)和inout(输入输出),建议把所有和AIDL相关的放在一个包中,便于复制到客户端使用。

服务端

// IRemoteService.aidl
package com.example.android;

// Declare any non-default types here with import statements

/** Example service interface */
interface IRemoteService {
    /** Request the process ID of this service, to do evil things with it. */
    int getPid();

    /** Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

android studio编译后生成IRemoteService.java(project/app/build/generated/source/aidl/debug/com/example/android/aidl/IRemoteService.java)

public class RemoteService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Return the interface
        return mBinder;
    }

    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getPid(){
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {
            // Does nothing
        }
    };
}

客户端

IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
    // 建立连接时
    public void onServiceConnected(ComponentName className, IBinder service) {
        // Following the example above for an AIDL interface,
        // this gets an instance of the IRemoteInterface, which we can use to call on the service
        mIRemoteService = IRemoteService.Stub.asInterface(service);
    }

    // Called when the connection with the service disconnects unexpectedly
    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        mIRemoteService = null;
    }
};
    //启动服务
    private void startService() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.android", "com.example.android.RemoteService"));
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

继承Parcelable接口的对象

//创建一个Book类,实现Parcelable接口
public class Book implements Parcelable {
    private int bookId;
    private String bookName;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
    }

    public Book() {
    }

    protected Book(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
    }

    public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel source) {
            return new Book(source);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                '}';
    }
}

创建与bean对象同名的aidl

//Book.aidl
package com.itzyf.aidl;
parcelable Book;

创建aidl服务端

package com.itzyf.aidl;

import  com.itzyf.aidl.Book;
interface IBookManager {
            List<Book> getBookList();
            void addBook(in Book book);
}

其他文件同其他基本类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值