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

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

问题描述

我需要在我的 Android 应用程序中每隔 20 分钟定期从服务器获取新闻/事件更新.AFAIK Intent Service 和 Broadcast Receiver 组合将比使用 Service 更好,因为我不打算与正在运行的 Service 进行通信.为了定期获取事件,我知道 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) 使用 Timer Task 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) 只需在应用程序启动时和 Intent Service onHandleIntent 方法中启动 Intent Service 即可启动 TimerTask ScheduleAtFixedRate.如果这是首选方式,我将如何以及何时取消计时器任务以及意图服务何时会被破坏.

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天全站免登陆