aidl通讯试所有的对象都得实现Parcelable 接口
onTransact() Binder 驱动执行完毕时需要回调的方法
Proxy 移动端IPC通讯的时候,消息都是通过Binder 进行传递的。
这个代理可以理解为:IPC机制中,Android代码 跟底层对应IPC的Binder驱动 之间进行通讯时的代理
asInterface() 查询本地所有的aidl 接口,判断该Binder对象是否为一个本地的 Binder引用
然后bindService的时候,需要传递一个 ServiceConnection的内部类实现,在ServiceConnection内部类实现中
bindService(intent, conn, BIND_AUTO_CREATE);
ServiceConnection conn=new ServiceConnection() {
//aidl链接失败回调
@Override
public void onServiceDisconnected(ComponentName name) {
}
//aidl连接成功回调
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//在系统内部将执行一下逻辑行为
/*public static com.aidl.lxcay.MyAidlInterface asInterface(android.os.IBinder obj) {
* if ((obj == null)) {
* return null;
* }
* 查询本地所有的aidl 的描述,判断该Binder对象是否为一个本地的 Binder引用
* android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
* if (((iin != null) && (iin instanceof com.aidl.lxcay.MyAidlInterface))) {
* 返回定义好的代理接口
* return ((com.aidl.lxcay.MyAidlInterface) iin);
* }
* 返回默认代理
* return new com.aidl.lxcay.MyAidlInterface.Stub.Proxy(obj);
* }
*/
//这个localInterface 就是aidl里面规定好的接口引用,即Binder 引用
localInterface = Stub.asInterface(service);
//在客户端可以通过点击事件执行这个引用里对应的函数,如果执行成功,会在相应的Service里面执行相应的 函数;
//下面给出一个demo
}
};
eg:
//客户端
public void click(View v) throws RemoteException{
Log.i(TAG, localInterface.login("24", 24)+"");
Log.i(TAG, localInterface.sayHello(new Person("lxcay", 24))+"");
}
//服务端
public class MyServices extends Service {
@Override
pub