写点什么

Android 进阶——Android 跨进程通讯机制之 Binder,okhttp 读取信息 kotlin

用户头像
Android架构
关注
发布于: 刚刚
  • Construct the stub at attach it to the interface.*/

  • public Stub() {

  • this.attachInterface(this, DESCRIPTOR);

  • }


/**


  • Cast an IBinder object into an com.handsome.boke.IMyAidlInterface interface,

  • generating a proxy if needed.*/

  • public static com.handsome.boke.IMyAidlInterface asInterface(android.os.IBinder obj) {

  • if ((obj == null)) {

  • return null;

  • }

  • android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);

  • if (((iin != null) && (iin instanceof com.handsome.boke.IMyAidlInterface))) {

  • return ((com.handsome.boke.IMyAidlInterface) iin);

  • }

  • return new com.handsome.boke.IMyAidlInterface.Stub.Proxy(obj);

  • }


@Override


public android.os.IBinder asBinder() {


return this;


}


@Override


public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {


switch (code) {


case INTERFACE_TRANSACTION: {


reply.writeString(DESCRIPTOR);


return true;


}


case TRANSACTION_basicTypes: {


data.enforceInterface(DESCRIPTOR);


int _arg0;


_arg0 = data.readInt();


long _arg1;


_arg1 = data.readLong();


boolean _arg2;


_arg2 = (0 != data.readInt());


float _arg3;


_arg3 = data.readFloat();


double _arg4;


_arg4 = data.readDouble();


java.lang.String _arg5;


_arg5 = data.readString();


this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);


reply.writeNoException();


return true;


}


case TRANSACTION_add: {


data.enforceInterface(DESCRIPTOR);


int _arg0;


_arg0 = data.readInt();


int _arg1;


_arg1 = data.readInt();


int _result = this.add(_arg0, _arg1);


reply.writeNoException();


reply.writeInt(_result);


return true;


}


}


return super.onTransact(code, data, reply, flags);


}


private static class Proxy implements com.handsome.boke.IMyAidlInterface {


private android.os.IBinder mRemote;


Proxy(android.os.IBinder remot


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


e) {


mRemote = remote;


}


@Override


public android.os.IBinder asBinder() {


return mRemote;


}


public java.lang.String getInterfaceDescriptor() {


return DESCRIPTOR;


}


/**


  • Demonstrates some basic types that you can use as parameters

  • and return values in AIDL.*/

  • @Override

  • public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException {

  • android.os.Parcel _data = android.os.Parcel.obtain();

  • android.os.Parcel _reply = android.os.Parcel.obtain();

  • try {

  • _data.writeInterfaceToken(DESCRIPTOR);

  • _data.writeInt(anInt);

  • _data.writeLong(aLong);

  • _data.writeInt(((aBoolean) ? (1) : (0)));

  • _data.writeFloat(aFloat);

  • _data.writeDouble(aDouble);

  • _data.writeString(aString);

  • mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);

  • _reply.readException();

  • } finally {

  • _reply.recycle();

  • _data.recycle();

  • }

  • }


@Override


public int add(int num1, int num2) throws android.os.RemoteException {


android.os.Parcel _data = android.os.Parcel.obtain();


android.os.Parcel _reply = android.os.Parcel.obtain();


int _result;


try {


_data.writeInterfaceToken(DESCRIPTOR);


_data.writeInt(num1);


_data.writeInt(num2);


mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);


_reply.readException();


_result = _reply.readInt();


} finally {


_reply.recycle();


_data.recycle();


}


return _result;


}


}


static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);


static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);


}


/**


  • Demonstrates some basic types that you can use as parameters

  • and return values in AIDL.*/

  • public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;


public int add(int num1, int num2) throws android.os.RemoteException;


}


1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521532、asInterface


当客户端 bindService 的 onServiceConnecttion 的回调里面,通过 asInterface 方法获取远程的 service 的。其函数的参数 IBinder 类型的 obj,这个对象是驱动给我们的,如果是 Binder 本地对象,那么它就是 Binder 类型,如果是 Binder 代理对象,那就是 BinderProxy 类型。asInterface 方法中会调用 queryLocalInterface,查找 Binder 本地对象,如果找到,说明 Client 和 Server 都在同一个进程,这个参数直接就是本地对象,直接强制类型转换然后返回,如果找不到,说明是远程对象那么就需要创建 Binder 代理对象,让这个 Binder 代理对象实现对于远程对象的访问


/**


  • Cast an IBinder object into an com.handsome.boke.IMyAidlInterface interface,

  • generating a proxy if needed.*/

  • public static com.handsome.boke.IMyAidlInterface asInterface(android.os.IBinder obj) {

  • if ((obj == null)) {

  • return null;

  • }

  • android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);

  • if (((iin != null) && (iin instanceof com.handsome.boke.IMyAidlInterface))) {

  • return ((com.handsome.boke.IMyAidlInterface) iin);

  • }

  • return new com.handsome.boke.IMyAidlInterface.Stub.Proxy(obj);

  • }12345678910111213143、add


当客户端调用 add 方法时,首先用 Parcel 把数据序列化,然后调用 mRemote.transact 方法,mRemote 就是 new Stub.Proxy(obj)传进来的,即 BinderProxy 对象


@Override


public int add(int num1, int num2) throws android.os.RemoteException {


android.os.Parcel _data = android.os.Parcel.obtain();


android.os.Parcel _reply = android.os.Parcel.obtain();


int _result;


try {


_data.writeInterfaceToken(DESCRIPTOR);


_data.writeInt(num1);


_data.writeInt(num2);


mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);


_reply.readException();


_result = _reply.readInt();


} finally {


_reply.recycle();


_data.recycle();


}


return _result;


}


1234567891011121314151617184、transact


BinderProxy 的 transact 方法是 native 方法,它的实现在 native 层,它会去借助 Binder 驱动完成数据的传输,当完成数据传输后,会唤醒 Server 端,调用了 Server 端本地对象的 onTransact 函数


public native boolean transact(int code, Parcel data, Parcel reply,int flags) throws RemoteException;125、onTransact

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android进阶——Android跨进程通讯机制之Binder,okhttp读取信息kotlin