没有合适的资源?快使用搜索试试~ 我知道了~
疯狂android资料:第十章Service与BroadcaseReceiver.doc
0 下载量 147 浏览量
2022-11-26
16:58:16
上传
评论
收藏 297KB DOC 举报
温馨提示
试读
14页
疯狂android资料:第十章Service与BroadcaseReceiver.doc
资源推荐
资源详情
资源评论
2013 年 1 月 28 日 23:10:36
2013 年 1 月 29 日 12:31:49
Service 是 android 四大组件中与 Activity 最相似的组件,它们都代表可执行的程序,Service
与 Activity 的区别在于:Service 一直在后台运行,它们没有用户界面,所以绝不会到前台来。
一旦 Service 被启动起来之后,他就与 Activity 一样。他完全具有自己的生命周期。关于程
序中 Activity 与 Service 的选择标准是:如果某个程序组件需要再运行时间向用户呈现某个
界面,或者该程序需要与用户交互,就需要使用 Activity,否则就应该考虑使用 Service 了。
开发者开发 Service 的步骤与开发 Activity 的步骤很像,开发 Service 组件需要先开发一个
Service 子 类 , 然 后 在 AndroidManifest.xml 文 件 中 配 置 该 Service , 配 置 时 刻 通 过
<intent-filter..../>元素指定他可被那些 Intent 启动。
Android 系统本身提供了大量的 Service 组件,开发者可通过这些系统 Service 来操作
Android 本身。
BroadcaseReceiver 组 件 就 像 一 个 全局 的事 件监 听器 ,只 不过 它用 于监 听系 统发 出的
Broadcase。通过使用 BroadcaseReceiver,即可在不同应用程序之间通信。
10.1Service 简介
Service 组件也是可执行的程序,他也有自己的生命周期。创建、配置 Service 与创建、配置
Activity 的过程基本相似
10.1.1 创建、配置 Service
就像开发 Activity 需要的两个步骤:
-》开发 Activity 子类
-》在 AndroidManifest 文件中配置 Activity
开发 Service 也需要两个步骤:
-》定义一个继承 Service 的子类
-》在 AndroidManifest 文件中配置该 Service
Service 与 Activity 还有一点相似之处,他们都是从 Context 派生出来的,因此他们都可调用
Context 里定义的如 getResource()、getContentResolver()等方法。
与 Activity 相似的是,Service 中也定义了系列生命周期方法,如下:
-》abstract IBinder onBind(Intent intent):该方法是 Service 子类必须实现的方法。该方法返
回一个 IBinder 对象,应用程序可通过该对象与 Service 组件通信。
-》void onCreate():当该 Service 第一次被创建后将立即回调该方法。
-》void onDestroy():当该 Service 被关闭之前将会回调该方法。
-》void onStartCommand(Intent intent,int flags,int startId):该方法的早期版本是 void onStart
(Intent intent,int startId),每次客户端调用 startService(Intent)方法启动该 Service 时都会
回调该方法。
-》boolean onUnbind(Intent intent):当该 Service 上绑定的所有客户端都断开连接时将会回
调该方法。
实例:定义一个 Service 组件(10.1.1)
当该 Service 开发完成之后,接下来可在程序中运行该 Service 了,Android 系统中运行
Service 有如下两个方式:
-》通过 Context 的 startService()方法:通过该方法启用 Service,访问者与 Service 之间没
有关联,即使访问者退出了,Service 仍然运行。
-》通过 Context 的 bindService()方法:使用该方法启用 Service,访问者与 Service 绑定在
一起,访问者一旦退出,Service 也就终止。
10.1.2 启动和停止 Service
实例:使用 Activity 作为 Service 的访问者,该 Activity 的界面中包含两个按钮,一个按钮
用于启动 Service,一个按钮用于关闭 Service。(10.1.1)
在 Activity 中启动 Service 时,如下方法会被调用:(startService()方法启动服务)
-》public void onCreate()
-》public int onStartCommand(Intent intent, int flags, int startId)
在 Activity 中关闭 Service 时,如下方法会被调用:
-》public void onDestroy()
多次启动同一个已有的 Service 组件将不会回调 public void onCreate()方法
10.1.3 绑定本地 Service 并与之通信
当程序通过 startService(intent) 和 stopService(intent)启动、关闭 Service 时,
Service 与访问者之间基本不存在太多的关联,因此 Service 和访问者之间无法进行通信、
数据交换。
如果 Service 和访问者之间需要进行方法调用或数据交换,则应该使用 bindService()
和 unbindService()方法启动、关闭 Service。
Context 的 bindService()方法的完整方法签名为:bindService(Intent service,
ServiceConnection conn,int flags),该方法的三个参数的解释如下:
-》service:该参数通过 Intent 制定要启动的 Service。
-》conn:该参数是一个 ServiceConnection 对象,该对象用于监听访问者与 Service 之
间的连接情况,当访问者与 Service 之间连接成功时将回调该 ServiceConnection 对象
的 onServiceConnected(ComponentName name,IBinder service)方法,当访问者与
Service 之 间 断 开 连 接 时 将 回 调 该 ServiceConnection 对 象 的
onServiceDisconnected(ComponentName name)方法
-》flags:指定绑定是否自动创建 Service(如果 Service 还未创建)。该参数可指定为 0
(不自动创建)或 BIND_AUTO_CREATE(自动创建)。
注意到 ServiceConnection 对象的 onServiceConnection 方法中有一个 IBinder 对象,
该对象即可实现与绑定 Service 之间的通信。
当开发 Service 类时,该 Service 类必须提供一个 IBinder onBind(Intent intent)
方法,在绑定本地 Service 的情况下,onBind(Intent intent)方法所返回的 IBinder
对 象 将 会 传 给 ServiceConnection 对 象 里 onServiceConnected ( ComponentName
name,IBinder service)方法的 service 参数,这样访问者就可通过该 IBinder 对象与
Service 进行通信。
实际上开发时通常会采用继承 Binder(IBinder 的实现类)的方式实现自己的 IBinder
对象。
实例:示范如何在 Activity 中绑定本地 Service,并获取 Service 的运行状态。该程序
的 Service 类需要“真正”实现 onBind()方法,并让该方法返回一个有效的 IBinder
对象 (10.1.2)
在 Activity 绑定指定 Service 时,如下方法会被调用:(bindService()方法启动服务)
Service 中方法:
-》public void onCreate()
-》public IBinder onBind(Intent intent)
Activity 中方法:
-》private ServiceConnection conn=new ServiceConnection()
{
public void onServiceConnected(ComponentName name, IBinder service)
}
剩余13页未读,继续阅读
资源评论
xinkai1688
- 粉丝: 335
- 资源: 8万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功