如何服务和广播接收器之间发送和获取数据? [英] How to send and get data between Service and BroadcastReceiver?

查看:113
本文介绍了如何服务和广播接收器之间发送和获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的previous问题:<一href=\"http://stackoverflow.com/questions/12858208/how-to-keep-and-access-data-in-broadcastreceiver\">How在广播接收器,以保持和访问数据?我有出色的答案,但在这里我重点关注这一点。

In my previous question: How to keep and access data in BroadcastReceiver? I've got excellent answers, but here I focus on this.

有是广播接收器,并有服务。我想从接收器发送一些数据服务和找回数据。启动传输的一方为接收器,而不是服务。

There is BroadcastReceiver and there is Service. I would like to send some data from the receiver to service and get the data back. The party which initiates the transfer is receiver, not the service.

有关的简单起见,假设为SCREEN_ON该接收机等待,然后要求的服务,这是用户的一个生日,而服务器将返回true或false。

For the sake of the simplicity, let's say that receiver waits for SCREEN_ON and then asks service "is this a birthday of the user", and server returns true or false.

如果我没有记错的话,我应该附上我的数据去服务于意图,并调用 startService ,但如何找回数据?

If I am not mistaken I should attach my data that goes to Service to the Intent, and call startService, but how to get data back?

推荐答案

您不能真正做到这一点,你所描述的方式。 @CommonsWare是正确的,您选择的架构是有缺陷的

You can't really do this the way you've described. @CommonsWare is correct, your chosen architecture is flawed.

广播接收器是短暂的。因此,他们不能作出任何异步调用,他们无法执行采取时间显著量的任何任务。你的广播接收器得到由某个事件触发的,如果你需要做的,由于该事件的任何工作显著量则需要对这项工作委托给服务

BroadcastReceivers are short-lived. Therefore they cannot make any asynchronous calls and they cannot perform any tasks that take a "significant amount of time". Your BroadcastReceiver gets triggered by a certain event and if you need to do any significant amount of work due to that event you will need to delegate that work to a Service.

一般情况下(例如:一个活动内),如果你想申请从服务信息你可以绑定至服务并进行同步调用服务请求所需的数据,或者你可以异步调用服务(即:发送意图来的话),然后收听返回的结果(使用广播接收器)。这些方法都不从广播接收器,因为广播接收器不能绑定到一个服务,也不能进行异步调用。

Normally (for example: within an Activity), if you wanted to request information from a Service you could either bind to the Service and make a synchronous call to the service to request the data you want OR you could call the service asynchronously (ie: send an Intent to it) and then listen for the returned result (using a BroadcastReceiver). Neither of these methods works from a BroadcastReceiver because a BroadcastReceiver cannot bind to a Service and it cannot make asynchronous calls.

Edit:从 Android文档复制相关资料仅供参考的位置

Copied relevant information from the Android documentation for reference here

接收器生命周期

A 广播接收器对象仅适用于呼叫的持续时间
  到的onReceive(上下文,意图)。一旦从这个您code回报
  功能,系统认为对象被完成,​​并且不再
  活动的。

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

这有你能在做什么重要的影响
  的onReceive(上下文,意图)实施:任何需要
  异步操作不可用,因为你需要
  从函数返回处理异步操作,但在
  这一点上,<​​code>广播接收器
不再有效,因此,
  系统是免费的异步操作之前杀死它的进程
  完成。

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

在特别的,你可能不会显示一个对话框或绑定到从服务
  内广播接收器。对于前者,你应该使用的
   NotificationManager API。对于后者,你可以使用
   Context.startService()将命令发送到服务。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

这篇关于如何服务和广播接收器之间发送和获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆