AIDL基本认识
Android使用的一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口,它是用于定义客户端/服务器通信接口的一种描述语言。可以将这种可以跨进程访问的服务称为AIDL(Android Interface Definition Language)。
设计优势:
IPC角度:
通过AIDL,可以让本地调用远程服务器的接口就像调用本地接口那么接单,让用户无需关注内部细节,只需要实现自己的业务逻辑接口,内部复杂的参数序列化发送、接收、客户端调用服务端的逻辑等等,都不需要去关心。便捷程度:
Android进程间不能用通常的方式去访问彼此的内存数据。需要把传递的数据解析成基础对象,使得系统能够识别并处理。(AIDL可有效的解决这类问题)
AIDL基本使用
用法总结
1.创建*.aidl文件;
注意:AIDL支持的数据类型
2.建立Service子类;
创建的服务类中创建一个内部类,实现由aidl文件生成的Java接口。
1.onBind方法必须返回MyServiceImpl类的对象实例,否则客户端无法获得服务对象;
(对客户端公开相关接口)
2.创建一个继承自IMyService.Stub的内部类;(用于实现***.aidl中定义的方法)
注意:
AndroidManifest.xml文件中需要配置当前MyServiceService类
3.客户端Client调用;
绑定服务时自动创建服务; (I***Service.Stub.asInterface() 获取远程服务端的对象)
使用参考
详见:
http://gityuan.com/2015/11/23/binder-aidl/
注意
Note:
Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
参考
google Android:
https://developer.android.google.cn/guide/components/aidl?hl=zh_cn