我创建了一个服务,它在后台线程上同步来自网络的数据,并希望在服务完成时通知列表活动,以便它可以更新其光标?最好的方法是什么?我正在考虑在服务完成时发送广播,但不确定这是否是最好的方法。服务完成后我需要重新查询光标,所以我不确定这是否适用于广播接收器?我已经有一段时间没有做过很多 Android 工作了,所以提前致谢。
I created a service which syncs data from the web on a background thread and want to notify a list activity when the service has completed so it can update it's cursor? What would be the best way to do this? I'm thinking of sending a broadcast when the service is done but not sure if that's the best way to do it. I need to requery the cursor when the service has finished so I'm not sure if that will work well with broadcast receivers? I haven't done alot of android in awhile so thanks in advance.
发布评论
评论(3)
在您的服务中使用
Handler
,当您的ListActivity
连接到服务时注册客户端;也就是说,在ListActivity
的onServiceConnected
方法中,向您的服务发送一条Message
,以便您能够跟踪连接的客户端。然后,您可以简单地在Service
中循环访问这些客户端,并在您想要通知您的Service
中发生某些情况时向它们发送Message
。关于ListActivity。有关更多信息,您可以查看我正在进行的项目中的代码:我的ListActivity
和我的Service
存根。简而言之,在您的
MainActivity
中,启动并绑定到您的服务:定义一个信使来响应来自服务的消息,例如:
然后编写您的服务连接代码,例如:
请注意
replyTo
是我们刚刚创建的信使。在您的 NetworkService 中,使用以下命令跟踪连接的客户端:
并创建处理程序,如下所示:
然后,当您想要将消息发送回 MainActivity 时,只需执行类似以下操作下列的:
Use a
Handler
in your service that registers a client when yourListActivity
connects to the service; that is, in youronServiceConnected
method in yourListActivity
, send aMessage
to your service that enables you to keep track of connected clients. Then you can simply loop through these clients in yourService
and send them aMessage
when something takes place in yourService
that you want to notify yourListActivity
about. For more information you can look at code in an on-going project of mine: myListActivity
and myService
stub.In short, in your
MainActivity
, start and bind to your service with:Define a messenger to respond to messages from the service like:
And then write your service connection code like:
Note that the
replyTo
is the messenger we just created.In your
NetworkService
, keep track of connected clients with:and create your handler like:
Then, when you want to send a message back to your
MainActivity
, just do something like the following:如果您已经在使用支持库,则可以使用 LocalBroadcastManager 返回到您的活动,该活动将侦听正在发送的广播。
使用 LocalBroadcastManager 可确保只有您自己的应用程序才能接收广播,因此您不必担心泄露私人数据或打开潜在的安全漏洞。
另请参阅:如何使用 LocalBroadcastManager?
编辑(09/2014):
更好的方法这将使用事件总线框架,例如 Otto (我的最喜欢的)或 GreenRobot/EventBus 以避免组件耦合得太紧密。
If you're already using the support library, you could just as easily fire a broadcast from the service using the LocalBroadcastManager back to your activity that would listen for the broadcast being sent.
Using LocalBroadcastManager ensures only your own application would ever receive the broadcast so you don't have to worry about leaking private data or opening up potential security holes.
Also see: how to use LocalBroadcastManager?
EDIT (09/2014):
A better way to do this would be to use an event bus framework like Otto (my favourite) or GreenRobot/EventBus to avoid coupling your components too tightly.
根据忙碌的程序员高级 Android 开发指南
As per The Busy Coder's Guide to Advanced Android Development