如何使用 PendingIntent 从服务到客户端/活动进行通信?
我一直在 Android 开发者网站上阅读以下文本,特别是在 Framework 下主题->服务->启动服务。
其中规定如下:
如果服务不提供绑定,则使用 startService() 传递的意图是应用程序组件和服务之间的唯一通信模式。但是,如果您希望服务发回结果,那么启动服务的客户端可以为广播创建一个 PendingIntent(使用 getBroadcast()),并将其传递到启动服务的 Intent 中的服务。然后,该服务可以使用广播来传递结果。
我对此有几个问题:
- 此文本是否都适用于
Service
和IntentService
? - 如何(按代码方式)从
Service
内实现这一点; 然后,服务可以使用广播来传递结果。并且提到的广播将在哪里将结果传递给原始客户端/活动?是否有一些方法应该被覆盖(例如onActivityResult()
)或其他方法?
I have been reading the following text on the Android Developers Site, specifically under the Framework Topics -> Services -> Starting a Service.
There it states the following :
If the service does not also provide binding, the intent delivered with startService() 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 PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.
I have a couple of questions regarding this :
- Does this text both apply to
Service
s andIntentService
s ? - How (codewise) should this be achieved from within the
Service
; The service can then use the broadcast to deliver a result. and also where would the mentioned broadcast deliver the result to the original client/activity? Is there some method that should be overwritten (likeonActivityResult()
) or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Question was asked few months ago, but in case anyone is still looking for answer I hope I can help.
在下面的示例中,我们有本地服务,负责执行一些耗时的操作。 Activity 向服务发出请求,但不绑定到服务 - 只是通过请求发送意图。此外,Activity 还包含 BroadcastReceiver 的信息,当服务完成所请求的任务时,应回调该信息。该信息通过 PendingIntent 传递。该服务在后台线程中处理任务,当任务完成时,服务会向 BroadcastReceiver 广播一个答案。
1.创建BroadcastReceiver子类:
当任务完成时,该广播接收器将从服务中收到通知。
2.创建服务
嗯,最重要的部分是在handleMessage()方法中。服务只是进行广播操作,将结果传递给广播接收器。
3.您还需要在 Manifest.xml 中注册您的广播接收器和服务
4。最后,从 Activity 向您的服务发出请求:
5.对原始客户/活动做出响应。
您可以拥有抽象活动,您的所有活动都将从该抽象活动中扩展。此抽象活动可以自动将自身注册/注销为广播接收器中的响应侦听器。实际上这里没有太多选项,但重要的是,如果您保留对活动的静态引用,那么您必须在活动被销毁时删除该引用。
问候,
坡道
Question was asked few months ago, but in case anyone is still looking for answer I hope I can help.
In the example below we have local service, responsible for performing some time-consuming operations. Activity makes the requests to the service, but does not bind to it - just sends the intent with request. Additionally, Activity includes the information of BroadcastReceiver that should be called back when service is done with the requested task. The information is passed by PendingIntent. The service handles the task in background thread and when task is finished, service broadcasts the BroadcastReceiver with an answer.
1. Create BroadcastReceiver subclass:
This broadcast receiver will be notified from service, when task is done.
2. Create Service
Well, the most important part is in handleMessage() method. Service simply makes the broadcasts operation for delivering results to Broadcast Receiver.
3. You also need to register your broadcast receiver and service in Manifest.xml
4. And finally, make request to your service from Activity:
5. Delivering response to original client/activity.
You can have abstract activity from which all your activities will be extending. This abstrct activity can automatically register/deregister itself as a response listener in broadcast receiver. Not many options here actually, but it is important that if you keep static references to your activity then you must remove the refernece when activity is destroyed.
Regards,
Ramps
正如此处所写
As written here
为了在服务和活动之间进行通信。您还可以使用官方 Android 示例中提到的 Binder
http://developer.android.com/reference/android/app/ Service.html#LocalServiceSample
有关详细说明,请参阅此答案
https://stackoverflow.com/a/36983011/4754141
In order to perform communication between service and activity. You can also use Binder as mentioned in Official Android Example
http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
For detail explanation see this answers
https://stackoverflow.com/a/36983011/4754141