首页 > 代码库 > binder对于boolean类型的传递

binder对于boolean类型的传递

通过写了一个AIDL文件,查看自动生成的java文件代码得出的结论:

假如有:  boolean loadNativeSharedLib(String libPathName);

 

则对应的binder通信的实际操作为:

写入:

@Override public boolean loadNativeSharedLib(java.lang.String libPathName) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();boolean _result;try {_data.writeInterfaceToken(DESCRIPTOR);_data.writeString(libPathName);mRemote.transact(Stub.TRANSACTION_loadNativeSharedLib, _data, _reply, 0);_reply.readException();_result = (0!=_reply.readInt());}finally {_reply.recycle();_data.recycle();}return _result;}

处理&返回:

case TRANSACTION_loadNativeSharedLib:{data.enforceInterface(DESCRIPTOR);java.lang.String _arg0;_arg0 = data.readString();boolean _result = this.loadNativeSharedLib(_arg0);reply.writeNoException();reply.writeInt(((_result)?(1):(0)));return true;}

需要重点关注:


将boolean转化为int: reply.writeInt(((_result)?(1):(0)));
将int 还原boolean: _result = (0!=_reply.readInt());

这是一种简洁有效地方式.

之所以分析这个,是因为: 1)在native code调用java的service时需要考虑传递序列. 2)不借助aidl工具生成时,也需要手动实现上述两个block的代码

binder对于boolean类型的传递