计时器任务VS报警管理器的使用在Android的服务 [英] Timer Task VS Alarm Manager usage in Android Service

查看:129
本文介绍了计时器任务VS报警管理器的使用在Android的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取定期就像在我的Andr​​oid应用程序每20分钟从服务器新闻/事件更新。 AFAIK意图服务和广播接收器的组合会比使用服务,因为我不是要与正在运行的服务,以更好地沟通。为了定期获取事件,我知道2个选项

I need to fetch news/event updates from the server at regular intervals like for every 20mins in my Android App. AFAIK Intent Service and Broadcast Receiver combination will be better than using Service As I am not going to communicate with the running Service. In order to fetch events at regular intervals I know 2 options

1)使用定时器任务ScheduleAtFixedRate,我要开始IntentService,这将再次与放取的活动;广播如有更新和自我毁灭。鉴于间隔后,IntentService将再次通过的TimerTask烧制

1) Using Timer Task ScheduleAtFixedRate, I am going to start IntentService, which will fetch events once & broadcast if any updates and destroy itself. After Given Interval, IntentService will be again fired by TimerTask

2)简单地在应用程序的开始,并在意向书服务onHandleIntent方法启动的TimerTask ScheduleAtFixedRate开始意图服务。如果这是preferred方式,如何当我取消定时任务,并当意图服务将被摧毁。

2) Simply starting Intent Service at the start of app and within Intent Service onHandleIntent method Starting a TimerTask ScheduleAtFixedRate. If this is preferred way, How and when I cancel Timer Task and When the Intent Service is going to get Destroyed.

或者我必须使用报警管理。请注意,只要我需要,我现在用的应用程序,这些更新,还我需要每次20〜30分钟更新不是为每1或3分钟。

or I have to use Alarm Manager. Note I need these updates as long as I am using the App, also I need updates for every 20 to 30 mins not for every 1 or 3 mins.

任何机构请建议我,先谢谢了。

Any body please suggest me, Thanks in Advance.

推荐答案

去AlarmManager。我曾尝试的TimerTask之前,它不会在某些设备正常工作,并在一段时间后会被杀掉。

Go for AlarmManager. I have tried TimerTask before, it does not work properly in some devices and get killed after some time.

这篇关于计时器任务VS报警管理器的使用在Android的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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