periodicaly运行的任务(每天一次/一周一次)任务、periodicaly

2023-09-12 06:27:38 作者:我不是你王子我只是你骑士

我要运行一些任务(即让我的网站的新闻网页)定期(每周一次/天),即使我的应用程序关闭。可能吗?

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?

推荐答案

的确是这样,你需要看的 AlarmManager 设置中反复出现的报警。这是更好地为电池寿命的装置上,作为不同于服务不会在后台不断地运行。报警触发广播接收机将执行您的自定义code。

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.

一个很好的例子可以发现,在后续的SO后:

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

报警管理举例