您现在的位置是:首页 > 文章详情

彻底弄懂binder与aidl

日期:2018-08-22点击:361

aidl与binder机制


为什么需要binder

没有用到binder之前,我们每个app生活在分配给自己的虚拟机和内存空间中,这样保证了app应用的安全,到很多时候我们需要用到跨进程通信(IPC),这时binder就为此而生。ActivityManagerService、WinderManagerService等系统服务的背后都是Binder。

binder跨进程通信原理

image

如上图,binder要想运作,必须有四个角色合作:

  • 客户端:获取服务端的binder驱动引用。并调用它的transact方法即可向服务端发送消息。
  • 服务端:指Binder实现类所在的进程,该对象一旦创建,内部则会启动一个隐藏线程,会接收客户端发送的数据,然后执行Binder对象中的onTransact()函数。
  • Binder驱动:当服务端Binder对象被创建时,会在Binder驱动中创建一个mRemote对象。
  • Service Manager:作用相当于DNS,就想平时我们通过网址,然后DNS帮助我们找到对应的IP地址一样,我们在Binder服务端创建的Binder,会注册到Service Manager,同理,当客户端需要该Binder的时候,也会去Service Manager查找。

运作流程:

  1. 服务端创建对应Binder实例对象,然后开启隐藏Binder线程,接收来自客户端的请求,同时,将自身的Binder注册到Service Manager,在Binder驱动创建mRemote对象。
  2. 客户端想和服务端通信,通过Service Manager查找到服务端的Binder,然后Binder驱动将对应的mRemote对象返回
  3. 至此,整个通信连接建立完毕

加强理解,先丢几张图:


image

首先客户端开启服务,与服务端连接,然后实例化ServiceConnection对象,得到服务端的IBinder实例化类(若是同一进程,则返回的是服务端的Binder类,若不是同一进程,则返回服务端的代理类,这个问题后面会提到),然后调用该IBinder实例化类里的方法,实际上是利用该Ibinder的transact()方法向服务端发送数据,然后服务端调用onTransact返回数据给客户端,这样就完成了一次服务端与客户端的通信,


aidl小栗子

下面这根据aidl小例子对上面的进行验证

  • 第一步:写aidl接口文件
 interface FoodInterface { //自带的,用于说明aidl支持的数据类型 void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); //自己添加的 int getFoodPrice(String foodName); } 
  • 第二步:make project一下工程,然后可以在该目录下看到生成的java接口文件,名称与aidl文件名一致。


    生成的java接口文件
  • 第三步:完善服务端,新建服务,重写onBinder方法,新建Binder内部类,继承的是接口名.Stub抽象类,重写里面的抽象方法,该方法就是接口声明的方法。


    完善服务端
  • 第四步:完善客户端,启动服务,获取服务端的(代理)类,并调用服务端的方法,返回服务端的结果完成通信。
 private ServiceConnection mServiceConnection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { service1= FoodInterface.Stub.asInterface(service); Toast.makeText(MainActivity.this,"绑定成功!", Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName name) { service1=null; Toast.makeText(MainActivity.this,"断开服务!", Toast.LENGTH_SHORT).show(); } }; 
 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String foodStr = MainActivity.this.food.getText().toString().trim(); int price= 0; try { price = service1.getFoodPrice(foodStr); } catch (RemoteException e) { e.printStackTrace(); } priceView.setText(foodStr+"的价格为"+price+"元"); } }); 

看效果


通信效果

接下来,我们来看一看系统帮我们生成的接口文件,下面是源码:

 public interface FoodInterface extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.example.wc.aidldemo.FoodInterface { private static final java.lang.String DESCRIPTOR = "com.example.wc.aidldemo.FoodInterface"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.example.wc.aidldemo.FoodInterface interface, * generating a proxy if needed. */ public static com.example.wc.aidldemo.FoodInterface asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.example.wc.aidldemo.FoodInterface))) { return ((com.example.wc.aidldemo.FoodInterface)iin); } return new com.example.wc.aidldemo.FoodInterface.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 { java.lang.String descriptor = DESCRIPTOR; 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_getFoodPrice: { data.enforceInterface(descriptor); java.lang.String _arg0; _arg0 = data.readString(); int _result = this.getFoodPrice(_arg0); reply.writeNoException(); reply.writeInt(_result); return true; } default: { return super.onTransact(code, data, reply, flags); } } } private static class Proxy implements com.example.wc.aidldemo.FoodInterface { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { 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 getFoodPrice(java.lang.String foodName) 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.writeString(foodName); mRemote.transact(Stub.TRANSACTION_getFoodPrice, _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_getFoodPrice = (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 getFoodPrice(java.lang.String foodName) throws android.os.RemoteException; } 

不得不说,谷歌封装的确实好,将服务端和客户端代码封装在一个接口文件里了。看一下这个接口的结构:

FoodInterface接口 { 抽象类Stub 继承Binder 实现FoodInterface{ 静态的asInterface方法{ 返回代理类(分两种情况:同进程,直接返回继承该Stub的Binder();不同进程,返回静态代理类) } 重写的onTransact方法 静态代理类Proxy 实现FoodInterface{ 实现getFoodPrice,主动调用transact方法 } } 声明getFoodPrice方法 } 

上面已经写的很清楚了,总的过程就是服务端继承stub静态类,重写接口声明的方法,然后根据服务端启动服务,根据接口.stub.asInterface获取(代理)binder对象,然后binder调用声明的方法(实际上是调用transact向服务端发送数据)服务端调用对应的方法,利用onTransact反馈数据完成通信。
感谢各位看官老爷,创作不易,麻烦动动手指点个赞!!
参考文章:
https://blog.csdn.net/cjh94520/article/details/71374872
https://blog.csdn.net/u012702547/article/details/52748403

原文链接:https://yq.aliyun.com/articles/634356
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章