网站大量收购独家精品文档,联系QQ:2885784924

第8章Services解析.ppt

  1. 1、本文档共25页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第8章Services解析

Service解析 * * QQ群:287966120 QQ号:1281147324 倚动软件工厂实验室 主要内容 Service简介 文本 什么是Service? A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background. Service是android 系统中的一种组件,它跟Activity的级别差不多,它们都是从Context派生出来的,但是它不能自己运行,只能在后台运行,并且可以和其它组件进行交互。它需要通过某一个Activity或者其他Context对象来调用。 继承Service类 与开发其它Android组件类似,开发Service组件需要先开发一个Service子类,该类需继承系统提供的Service类,系统中Service类包含的方法主要有: abstract IBinder onBind(Intent intent):该方法是一个抽象方法,所有Service子类必须实现该方法。该方法将返回一个IBinder对象,应用程序可通过该对象与Service组件通信; void onCreate():当Service第一次被创建时,将立即回调该方法; void onDestroy():当Service被关闭之前,将回调该方法; void onStartCommand(Intent intent,int flags,int startId):每次客户端调用startService(Intent intent)方法启动该Service时都会回调该方法; boolean onUnbind(Intent intent):当该Service上绑定的所有客户端都断开连接时将会回调该方法。 配置Service 开发Service组件需要先开发一个Service子类,然后在AndroidManifest.xml文件中配置该Service,配置时可通过intent-filter…/元素指定它可被哪些Intent启动。 在AndroidManifest.xml文件中配置Service: service android:name=“.MyService” intent-filter action android:name=“.android.MyService”/ /intent-filter /service Action中的值一定要与程序中创建的Intent的Action的值一致,程序就是根据Action的值来查找相应的Service从而启动它。 Service的启动 通过Context的startService()启动Service后,访问者与Service之间没有关联,该Service将一直在后台执行,即使调用startService()的进程结束了,Service仍然还存在,直到有进程调用stopService(),或者Service自己自杀(stopSelf())。这种情况下,Service与访问者之间无法进行通信、数据交换。 通过Context的bindService()绑定Service后,Service就和 调用bindService()的组件同生共死了,也就是说当调用 bindService()的组件销毁了,那么它绑定的Service也要跟着被结束。 绑定Service Context的bindService()方法的完整方法签名为:bindService(Intent service,

文档评论(0)

5500046 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档