Android Interface Definition Language
服务器端
1、建一个.aidl结尾的文件。在里边定义接口。接口名字与文件名字相同,假设接口为
Interface MyServer { String write();}
2、Build工程后在gen目录下会生成一个MyServer。Java文件, 其中包含实现接口Myserver的类 Stub 。
3、新建一个继承 Myserver。Stub 的类,假设名字为MyServerImpl。实现 MyServer接口的方法 write();
4、新建一个MyService 类继承 Service。在onBind 的方法中返回一个 MyserverImpl实例. 并在 AndroidManifest 中的service intentFilter属性中添加 <action> 假设 action 值为 "com.test.myserver"
客户端
1、新建一个Acitivity ,将服务器端的 。aidl文件连同包一起拷贝到 客户端的src目录下。Build工程生成一个与 服务器端 一样的 MyServer。Java文件。
2、在Activity中创建 ServiceConnection 在 onServiceConnected 中通过
MyAIDL.Stub.asInterface(service) 获取 MyServer 接口实例。
3、绑定 服务器端 MyService 在 Intent 的action 中写入 服务器 清单中 service中的 action。