android Alarmmanager 每月启动一次

发布于 2024-10-29 04:30:17 字数 263 浏览 1 评论 0原文

我希望 AlarmManager 每个月 5 日下午 3 点执行一项任务。我该怎么做呢?

  • 首次启动取决于用户安装应用程序的时间。好吧,我可以做一些数学计算,计算下个月 5 号需要多少毫秒,但我希望有一种更简单的方法

  • 重复选项让我非常头痛。我只能设置一个固定的间隔。但两个月之间的时间并不固定,因为有时有 30 天,有时有 31 天(更不用说 2 月了)。

我该如何做到最好?

问候, 一个。

I want the alarmManager to perform a task every 5th of the month at 3pm. How would I do that?

  • first start depends on when the user installs the app. Okay I can do some math calculating how many ms it takes to the next 5th of the month, but I hope there is an easier way

  • the repeat option gives me a real headache. I only can set one fixed intervall. but the time between two month is not fixed since sometimes I have 30, sometimes 31 days (not to mention Feb).

How would I do this best?

Regards,
A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-11-05 04:30:17

您可以更简单地做到这一点,看看 Date 类:
http://developer.android.com/reference/java/util/Date.html

并尝试类似 Pseudo:

// This month 5th
Date date = new Date();
date.setDay(5);
// Next month the 5th
Date nextDate = new Date();
nextDate.setDay(5);
nextDate.setMonth(date.getMonth()+1);


到了年底,你可能会遇到问题,但问题比现在要少。

还有其他日期 API,其中一些可以为您解决年份问题。看看周围吧。

编辑

刚刚注意到不推荐使用日历:

http:// developer.android.com/reference/java/util/Calendar.html

Calendar now = Calendar.getInstance();
now.setField(Calendar.DAY_OF_MONTH, 5);
// Next month the 5th
Calendar nextDate = new = Calendar.getInstance();
nextDate.setField(Calendar.DAY_OF_MONTH, 5);
nextDate.setField(Calendar.MONTH_OF_YEAR, date.getField(Calendar.MONTH_OF_YEAR)+1); // issues in december

You can do this much simpler, have a look at the Date class:
http://developer.android.com/reference/java/util/Date.html

And Try something like, Pseudo:

// This month 5th
Date date = new Date();
date.setDay(5);
// Next month the 5th
Date nextDate = new Date();
nextDate.setDay(5);
nextDate.setMonth(date.getMonth()+1);


You would have issues when you come to end of the year, but it's less issues than you have at the moment.

There are other Date API's and some will fix the year problem for you. Just have a look around.

EDIT

Just noticed that's deprecated use Calendar:

http://developer.android.com/reference/java/util/Calendar.html

Calendar now = Calendar.getInstance();
now.setField(Calendar.DAY_OF_MONTH, 5);
// Next month the 5th
Calendar nextDate = new = Calendar.getInstance();
nextDate.setField(Calendar.DAY_OF_MONTH, 5);
nextDate.setField(Calendar.MONTH_OF_YEAR, date.getField(Calendar.MONTH_OF_YEAR)+1); // issues in december
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文