定期将数据从服务发送到绑定的活动 [英] Send data from service to bound activity periodically

查看:90
本文介绍了定期将数据从服务发送到绑定的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动并绑定到服务的活动.它发送带有数据列表的意图,并且该服务负责定期更新该数据.这是在handleIntent(Intent)方法中完成的.我要做的是将更新的数据发送回活动.活动和服务都在同一个应用程序中.如何收听"我的服务请求?我必须使用Messenger和/或Broadcast Receiver吗?最干净,最简单,最有效的方法是什么?谢谢.

I have an Activity that starts and binds to a service. It sends an intent with a List of data and the service is responsible for periodically updating that data. This is done in the handleIntent (Intent) method. What I want to do is send the updated data back to the activity. Both the activity and the service are in the same application. How can I "listen" for requests from my service? Do I have to use a Messenger and/or Broadcast Receiver? what's the cleanest, easiest, most efficient way of doing this? Thanks.

推荐答案

我建议您检查 EventBus .它可以让您发送本地广播接收器之类的事件.从活动中,您可以监听特定事件并显示结果.您甚至还可以使用接口从服务中获取回调.

I suggest you check EventBus. it will allow you to send events like local broadcast receiver. And from that in activity you can listen specific event and display result. you can even use interface to get callback from service also.

没有任何库,您可以使用LocalBroadcastManager来实现. http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Without any library you can achieve this with LocalBroadcastManager. http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

这篇关于定期将数据从服务发送到绑定的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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