计算警报管理器触发的 3 天

发布于 2024-11-17 00:38:28 字数 295 浏览 3 评论 0原文

好的,所以我有一个闹钟管理器,我必须每三天关闭一次,当它关闭时,我将当前系统时间写入共享首选项,这样如果手机关闭,我就会知道它上次启动的时间。

我的问题是,由于我只能在触发时写入共享首选项,因此如何计算手机关闭后从上次触发后的 3 天?

我在想这样的事情

long refresh = lastTime + (360000*24)*3;

,lastTime 是最后一次触发的时间,但是如果手机在这段时间重新启动,那么从重新启动开始不会再有 3 天的时间,还是我认为这是错误的?

ok so I have an alarm manager that I have to go off every three days, when it goes off I write the current system time to shared preferences so that if the phone is turned off I have when it was last fired.

my question is how do I calculate 3 days from when it was last fired after the phone has been turned off since I can only write to shared preferences when its fired?

I was thinking something like this

long refresh = lastTime + (360000*24)*3;

where lastTime is when it was last fired but if the phone was restarted between then wouldnt it be another 3 days from that restart or am i thinking this wrong?

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

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

发布评论

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

评论(1

七月上 2024-11-24 00:38:28

您缺失了零(一天有 86400000 毫秒),但除此之外您的方法看起来不错(假设 lastTime 只是来自 System.currentTimeMillis() 的存储值)代码>)。 AlarmManager 使用绝对时间作为触发时间。

如果手机已关闭超过 3 天,那么当您恢复闹钟时,闹钟将过期并立即触发。需要注意的一件事是,如果您以这种方式恢复重复警报,并且自上次触发以来已经过去了 5 天,那么当您使用过去的日期恢复它时,它将立即触发(逾期的第 3 天警报)并且然后一天后再次触发(第 6 天警报)。您可能需要对此进行调整。

You have a zero missing (there are 86400000 milliseconds in a day), but other than that your approach looks fine (assuming that lastTime is just a stored value from System.currentTimeMillis()). AlarmManager uses absolute times for the trigger time.

If the phone has been turned off for more than 3 days then when you restore the alarm it will be overdue and will fire immediately. One thing to be aware of is that if you restore a repeating alarm in this way and it's been 5 days since it last fired, then when you restore it using a date in the past it will fire immediately (the overdue day 3 alarm) and then fire again after one more day (the day 6 alarm). You may want to adjust for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文