一次AIDL实战:单向传递和双向传递数据过程

项目中用到做个记录(引用链接在文章后)

支持的什么类型

  • Java基本数据类型
  • String
  • CharSequence
  • android.os.Parcelable
  • java.util.List
  • java.util.Map
  • AIDL中申明的接口
  • 实现Parcelable的类

需要引入全路径的类型

  • AIDL中申明的接口
  • 实现Parcelable的类

服务端:
1.格式:在包名同级目录下新建aidl文件,定义需要传递的接口,特殊类型注意引入路径,多种数据类型如下(https://www.jianshu.com/p/3783bdf9cabe)

  • MainBean .class
public class MainBean implements Parcelable {
    private int id;
    private boolean normalStatus;
    private long gmtCreated;
    private String name;
    private BigDecimal price;
    private SecondBean secondBean;
    private List<SecondBean> secondBeanList;
    private int[] numberArray;

    public MainBean () {

    }

    //固定写法, 只用修改Creator的泛型
    public static Creator<MainBean> getCREATOR() {
        return CREATOR;
    }

    //固定写法, 只用修改Creator的泛型
    public static final Creator<MainBean> CREATOR = new Creator<MainBean>() {
        @Override
        public MainBean createFromParcel(Parcel in) {
            return new MainBean(in);
        }

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

    @Override
    public int describeContents() {
        //固定写法
        return 0;
    }

    protected MainBean(Parcel in) {
        //单独写了一个readFromParcel和writeToParcel对应, 便于理解
        readFromParcel(in);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
       //切记非常重要, 写数据(out.write...)和下面的读数据(in.read...)的顺序一定要和声明的属性顺序对应上, 不然会因为读写的数据类型不一致而报异常

        //普通数据类型int, double, String等等, 用法以此类推
        out.writeInt(id);
        //由于没有写入布尔数据类型的方法, 需要把布尔数据类型转换为String
        out.writeString(String.valueOf(normalStatus));
        out.writeLong(gmtCreated);
        out.writeString(name);
        //判空, 因为如果price为null, 也会报异常, 没有写入BigDecimal的数据类型, 需要转为String
        out.writeString(price == null ? "0" : price.toString());
        #//写入对象, 该对象必须也要实现Parcelable接口
        out.writeParcelable(secondBean, flags);
        //写入List集合
        out.writeList(secondBeanList);
        //写入int类型数组, 其他的数组类型以此类推
        out.writeIntArray(numberArray);
    }

    private void readFromParcel(Parcel in) {
        #//顺序一定要对应上
        id = in.readInt();
        //读取的String转为布尔类型
        normalStatus = Boolean.valueOf(in.readString());
        gmtCreated = in.readLong();
        name = in.readString();
        //读取的price为String类型, 需要转为BigDecimal
        price = new BigDecimal(in.readString());
        //读取对象实现了Parcelable接口的对象
        secondBean = in.readParcelable(SecondBean.class.getClassLoader());
        //读取实现了Parcelable接口的对象List
        secondBeanList = in.readArrayList(SecondBean.class.getClassLoader());
        //创建(读取)int类型数组, 其他数组类型以此类推
        numberArray = in.createIntArray();
    }

    setMethod()...
    getMethod()...
  • SecondBean.class
public class SecondBean implements Parcelable {
    private int id;

    public SecondBean() {
    }

    public SecondBean(int id){
        this.id = id;
    }

    protected SecondBean(Parcel in) {
        id = in.readInt();
    }

    public static final Creator<SecondBean> CREATOR = new Creator<SecondBean>() {
        @Override
        public SecondBean createFromParcel(Parcel in) {
            return new SecondBean(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel out, int flag) {
        out.writeInt(id);
    }

    public static Creator<SecondBean> getCREATOR() {
        return CREATOR;
    }

    setMethod()...
    getMethod()...
}
  • IMyAidlCallback.class
import xxx.xxx.xxx.SecondBean;
import xxx.xxx.xxx.MainBean;

interface IMyAidlCallback {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void Success(in MainBean  mainBean);
    void Error(int code, in SecondBean secondBean);
}
  • IMainService.class
public class IMainService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return mIMyAidlListener;
    }

    IMyAidlCallback.Stub mIMyAidlListener = new IMyAidlCallback.Stub() {

        @Override
        public void Success(MainBean  mainBean) throws RemoteException {
        	//得到客户端返回的数据做自定义操作
        }

        @Override
        public void Error(int code, SecondBean secondBean) throws RemoteException {
            //得到客户端返回的数据做自定义操作
        }
    };

}

客户端
连接服务端,连接成功后进行通信

Intent intent = new Intent();
intent.setComponent(new ComponentName(包名, 路径+IMainService.class));
bindService(naviIntent, serviceConnection, Context.BIND_AUTO_CREATE);

ServiceConnection serviceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {       
       mIMyAidlInterface = IMyAidlCallback.Stub.asInterface(service);
       //得到引用后可以条用success或者error传递给服务器信息
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    	//还原xxxx
    }
};

如果服务端想给客户端发消息怎么办?难道服务端绑定一个客户端起的aidl服务?每次传递的数据类型都不同,能否使用泛型来传值?
1.客户端想从服务端那到值,服务端想在任意事件下发通知到客户端,那么这不就是我们常用的callback回调吗,客户端注册并实现callback,服务端收到客户端的注册请求,开启通知模式。这种方法也适用于一个服务多个客户端交互。
2.见示例如下:

服务端

interface IMainInfoInterface {

    //poi搜索结果, IMyAidlCallback 就是上面单向传递的接口
    void send(String to, String msg);
    void register(String source, IMyAidlCallback callback);
    void unRegister(String source);
}
	//利用注册通知的方式,来对服务端客户端实现双向通信
    IMainInfoInterface.Stub mIMainBeanListener = new IMainInfoInterface.Stub() {

        @Override
        public void send(String to, String msg) throws RemoteException {          
            if (map.containsKey(to))
                map.get(to).Success(msg);
        }

        @Override
        public void register(String source, IMyAidlCallback callback) throws RemoteException {
            map.put(source, callback);
        }

        @Override
        public void unRegister(String source) throws RemoteException {
            map.remove(source);
        }
    };

客户端

public class MainActivity extends AppCompatActivity implements IMyAidlCallback {

    private IMainInfoInterface mIMainInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerPoiReceiver();

        Intent intent = new Intent();
        naviIntent.setComponent(new ComponentName(xxx, xxx.class));
        bindService(intent, NaviServiceConnection, Context.BIND_AUTO_CREATE);
    }

    ServiceConnection NaviServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: ");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    public void Success(String msg) throws RemoteException {
        Log.i(TAG, "Success: " + msg);
    }

    @Override
    public void Error(String msg) throws RemoteException {
        Log.i(TAG, "Error: " + msg);
    }

    @Override
    public IBinder asBinder() {
        return mIPoiInterface.asBinder();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();     
        stopService(new Intent(xxx, xxx.class));
    }
}

ok,这就是一次aidl从单向到双向的一次交互过程,如有遗漏错误部分请看到后帮忙指出,一起学习~

你可能感兴趣的:(移动开发)