通过AIDL进程间通信
2016-03-11 23:57
411 查看
AIDL只不过是一种简化代码书写的方法,进程间通信实际上是由binder机制实现,AIDL本身不具备进程间通信的功能。
通俗点来说,binder机制的使用:大家(客户端和服务端)都定义有相同的AIDL接口,你实现的那部分接口由我来调用,我实现的由你调用;
那如何实现方法的调用呢,本质来说就是将各自的对象传递过去(Service-->Client : onBind()返回的Binder转化为AIDL接口所属的实例;Client-->Service:client的实例作为参数传入由client调用的方法中)
通俗点来说,binder机制的使用:大家(客户端和服务端)都定义有相同的AIDL接口,你实现的那部分接口由我来调用,我实现的由你调用;
那如何实现方法的调用呢,本质来说就是将各自的对象传递过去(Service-->Client : onBind()返回的Binder转化为AIDL接口所属的实例;Client-->Service:client的实例作为参数传入由client调用的方法中)
//服务端 //AIDL文件 package com.leo.aidl; interface infa { methodInvokedByClient(Object o); methodInvokedByService(); } //服务端Service public class sv extends Service { //用于获得客户端创建的实例,从而调用由客户端实现的方法 Object o; //实现infa接口的Binder类 private Binder mBinder = new infa.Stub() { //实现methodInvokedByClient public void methodInvokedByClient(Object o){..,this.o = o, ..};//<span style="font-family: Arial, Helvetica, sans-serif;">client的实例作为参数传入由client调用的方法中</span><div> </div> } //onBind返回mBinder public IBinder onBind(Intent intent) { return mBinder;} //调用由客户端实现的方法methodInvokedByService() o.methodInvokedByService(); }
//客户端 //AIDL文件 package com.leo.aidl; interface infa { methodInvokedByClient(Object o); methodInvokedByService(); } //客户端activity public class a extends Activity { Object o ; protected void onCreate(...) { //请求连接服务端 bindService(.. , mConnection ,...); } private ServiceConnection mConnection = new ServiceConnection() { //onBind被调用后,系统会回调onServiceConnection public void onServiceConnected(... , IBinder service) { //将Binder转化为infa的实现类对象 infa i = infa.Stub.asInterface(service); //此时客户端就可以调用服务端的方法了,同时也将客户端定义的实例传输到了服务端 i.methodInvokedByClient(o); } } //实现methodInvokedByService public void methodInvokedByService() {...}; }
相关文章推荐
- open failed: EACCES (Permission denied)
- atexit()函数(使main函数之后可以执行其他函数)
- [POJ 3007]Organize Your Train part II[Hash][Trie]
- Codeforces Round #345 (Div. 2)——B. Beautiful Paintings(贪心求上升序列个数)
- Again Twenty Five!
- codeforces--651B Beautiful Paintings
- CodeForces-630 A. Again Twenty Five! 【规律】
- Atitit atiMail atiDns新特性 v2 q39
- Atitit atiMail atiDns新特性 v2 q39
- Atitit atiMail atiDns新特性 v2 q39
- poj1995——Raising Modulo Numbers(快速幂取余)
- A. Again Twenty Five!
- 安装sklearn时出现 "ImportError: DLL load failed" 的解决方法
- offsetof与container_of宏[总结]
- 编译sakai10.6
- 机器学习实战--naive bayes和logistic Regression
- 精通 Grails: 构建您的第一个 Grails 应用程序
- aidl深度解析
- TIME_WAIT是什么?http连接
- dispatch_group_enter、 dispatch_group_leave和dispatch_group_wait来实现同步