定期运行任务(一天一次/一周一次) [英] Running task periodicaly(once a day/once a week)

查看:49
本文介绍了定期运行任务(一天一次/一周一次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定期(每周/每天一次)运行一些任务(即获取我的网站新闻页面),即使我的应用程序已关闭.是否可以?

解决方案

是的,您需要查看 AlarmManager 以设置重复出现的警报".这有利于设备的电池寿命,因为与服务不同,它不会在后台持续运行.警报触发 广播接收器,它将执行您的自定义代码.>

最后一点 - 闹钟的时间有枚举值,包括每天、半天等等,但您可以只设置一个实际值.

可以在以下 SO 帖子中找到一个很好的例子:

报警管理器示例

更新

更新的功能已添加到 Android 中.如果您正在阅读本文,那么我建议您现在查看 GcmNetworkManager.这可以优化电池寿命并在棒棒糖之前工作.对于 Lollipop,您可以使用 JobScheduler.我建议在 AlarmManager 上使用这些类.

I want to run some task (i.e. get my web site news page) periodically (once a week/ a day), even if my application is closed. Is it possible?

解决方案

Yes it is, you need to look at the AlarmManager to setup a reoccurring "Alarm". This is better for battery life on the device, as unlike a service it does not run constantly in the background. The Alarm triggers a broadcast receiver which will execute your custom code.

As a final note - there are enum values for the timing of the Alarm including daily, half daily and many more although you can just set an actual value.

A good example can be found in the follow SO post:

Alarm Manager Example

Update

Newer features have been added to Android. If you are reading this then I would advise you now look into GcmNetworkManager. This optimises battery life and works pre-lollipop. For Lollipop onwards you can use JobScheduler. I would advise using these classes over the AlarmManager.

这篇关于定期运行任务(一天一次/一周一次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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