1、背景
Android的IPC框架仰赖单一的IBinder接口。此时Client端调用IBinder接口的transact()函数,透过IPC机制而调用到远方(Remote)的onTransact()函数。

在client端transact()函数中根据code的值进行编码,在service端ontansact()函数中根据传过来的code值进行解码,加大了app开发的负担。
2、Proxy_Stub设计模式
采用Proxy-Stub设计模式将IBinder接口包装起来,让App与IBinder接口不再产生高度相依性。
Proxy类提供较好用的IA接口给Client使用。 Stub类别则是屏蔽了Binder基类的onTransact()函数,然后将IA接口里的f1()和f2()函数定义为抽象函数。于是简化了App开发的负担。

3、由APP开发者来写Proxy和Stub类





PlayerStub类将onTransact()函数隐藏起来,提供一个更具有美感、更亲切的新接口给mp3Binder类使用。隐藏了onTransact()函数之后,mp3Binder类的开发者就不必费心去了解onTransact()函数了。于是,PlayerProxy与PlayerStub两个类遥遥相对,并且将IPC细节知识(例如transact()和onTransact()函数之参数等)包夹起来。
4、由AIDL工具生成Proxy和Stub类
AIDL的目的是定义Proxy/Stub来封装IBinder接口,以便产生更亲切贴心的新接口。所以,在应用程序里,可以选择使用IBinder接口,也可以使用AIDL来定义出新接口。
网友评论