




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、B06_a活用IBind基e於軟r接硬整合口觀點于短(近)程通信(a)By 高煥堂內容1. 在同一进程里,活用IBinder接口2. 目的、议题与方法3. 留意线程的角色1、在同一基於进軟硬程整合觀里點 ,活用IBinder接口下图:myActivity与myService两类别是在同一个进程里执行IPCIPC 议题1.myActivity对象是谁创建的呢?2.myService对象是谁创建的呢?3. 当myService类里有个f1()函数,如何去调用它呢?4. 必须先取得myService对象的指针,才能调用f1()函数去存取对象的属性(Attribute)值。5. 那么,该如何才能取得m
2、yService对象的指 针呢?6. 想一想,可以透过myService类的静态(static)属性或函数来取得myService对象的指针吗?7. 可以透过IBinder接口来取得myService对象的指针吗? IBinder接口的重要目的是支持跨进程的远程调用。然而,它也应用于同一进程里的近程调用。 例如,当Activity远程调用Service时,我们常用bindService()函数去绑定Service,取得对方的IBinder接口。 在近程(同一进程内)调用时也可以使用bindService()函数去绑定Service,并取得对方的IBinder接口。 IBinder接口的典型实现
3、类是Binder基类,其定义于Binder.java里。 近程通信(同一进程里)如何使用IBinder接口呢? 举例说明之。 例如,myActivity和myService两者都执行于同一个进程(process)里,而且myActivity提供一个IS接口,其定义如下:interface IS void f1();void f2();现在,myActivity想要透过此IS接口来调用myService的函数;如下图:基於軟硬整合觀點2、目的、议题与方法 目的、议题与方法 目的:myActivity想去直接(近程)调用myService类的函数, 例如IS接口里的f1()函数 议题:如何取的my
4、Service对象的IS接口呢? 方法:先取得myService对象的IBinder接口步骤是:Step-1. myActivity透过bindService()函数来绑定(Bind)此myService。Step-2. myService回传myBinder类的IBinder接口给myActivity。Step-3. myActivity将IBinder接口转换为myBinder类的接口Step-4. myActivity调用myBinder类的getService()函数,取得myService的IS 接口。Step-5. 于是,myActivity就能调用IS接口(由myService类
5、实现)的函数了。在Android 说明文件里,说明道: “If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the Binder class and returning antance of it from onBind().The client receives the Binder and can use it to dire
6、ctly access public methods available in eitherthe Binder implementation or even the Service.” 依据上述文件的说明:“ you should create your interface byextending the Binder class and returningantance of it from onBind().”依据这个设计图,就来撰写myService类别如下:/ myService.java/ .public class myService extends Service implem
7、ents IS private final IBinder mBinder = new myBinder();/ Overridepublic IBinder onBind(Intent intent) return mBinder;/public class myBinder extends Binder IS getService() return myService.this;public void f1()/. Public void f2() / myActivity.java/.public class myActivity extends Activity IS isv; Ove
8、rrideprotected void onCreate(Bundle saved/.tanceState) Intent intent = new Intent(this, myService.class); bindService(intent, mConnection,Context.BIND_AUTO_CREATE);private ServiceConnection mConnection = new ServiceConnection() Overridepublic void onServiceConnected(ComponentName className,IBinder i
9、binder) myBinder ib = (myBinder)ibinder;isv = ib.getService();public void onClick(View v) / 例如:isv.f1()当Android框架启动myService时,就立即执行:private final IBinder mBinder = new myBinder();這诞生了myBinder对象。第1步 随后,当myActivity调用bindService()时, 框架会反向调用到myService的onBind()函数:public IBinder onBind(Intent intent)return mBinder;其将myBinder的IBinder接口回传给框架,并由框架调用onServiceConnected()函数, 将此接口回传给myActivity。第2步 由于myActivity与myService在同一个进程里执行,myActivity所获得的就是myBinder的真正接口(不是它的Proxy的); 于是,执行:myBinder ib = (myBinder) ibinder; 就从接获的IBinder接口转型(casting)为myBinder本身
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 警局逻辑考试题及答案
- 返工维修房屋合同范本
- 违法转包合同解除协议
- 酒吧安全责任合同范本
- 自家酒厂销售合同范本
- 门面联营分红合同范本
- 转让出租餐厅合同范本
- 软件技术赞助合同范本
- 矿山承包销售合同范本
- 民间借款房产合同范本
- 新疆吐鲁番地区2025年-2026年小学六年级数学阶段练习(上,下学期)试卷及答案
- TCT.HPV的正确解读课件
- 白酒生产安全员考试题库及答案解析
- 广东春考试卷及答案
- 《树之歌》课件 小学部编版语文二年级上册
- 画廊与画家签约合同范本
- 值勤岗亭安装方案范本
- 2025-2026冀人版三年级科学上册教学设计(附目录)
- 田径百米教学课件
- 小学劳动教育教学设计案例
- 大模型概念、技术与应用实践 课件 第6章 智能体
评论
0/150
提交评论