现在的位置: 首页 > 综合 > 正文

怎样使用PendingIntent让Service和Client进行交互

2013年01月19日 ⁄ 综合 ⁄ 共 908字 ⁄ 字号 评论关闭

熟悉Android的朋友都知道使用bindService()启动Service可以通过IBinder对象实现Service和Client端通信,本篇文章并不是介绍启动Service的常见方法(startService()和bindService()),如果想知道Service的基本运用,请绕道……

本篇文章主要讲述一种并不是很常见的启动Service的方式,并与Service端进行交互的实现方式,准备好了吗?现在开始我们的探索之旅!

 俗话说,众里寻他千百度,蓦然回首那人却在灯火阑珊处。运用到学习知识上面也同样如此,为了探索PendingIntent启动Service的实现方式,搜索了很多资料都没有找到满意的结果。最终还是决定再撸一篇Android文档,终于皇天不负有心人,在文档中找到了相关的说明(Website:Android说明文档),因此建议大家多看Android说明文档,学会学习第一手知识的方法,也就是所说的多穿一手鞋。

以下是从文档中摘录的一段话(只要讲述了可以使用PendingIntent+Broadcast的方式实现Client和Service的交互):

If the service does not also provide binding, the intent delivered withstartService()is
the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a
PendingIntentfor
a broadcast (with
getBroadcast())
and deliver it to the service in the
Intentthat starts the service. The
service can then use the broadcast to deliver a result.

 以上讲了那么多,现在来看看具体的实现方式吧!

 未完待续,敬请期待!

 

抱歉!评论已关闭.