如何处理android中每天触发的闹钟

发布于 2024-08-19 02:35:24 字数 481 浏览 5 评论 0原文

我想在我的应用程序中设置一个每天触发的闹钟。根据文档,我必须设置一次性闹钟,并在将接收闹钟信号的广播接收器中,重置后一天的闹钟。 这是正确的吗?

我的 BroadcastReceiver 可以很好地处理唤醒锁并启动释放此唤醒锁的服务。这里一切正常。

但是我有问题。在我的应用程序中,有一个复选框,当警报响起时会选中该复选框。要知道我的闹钟是否响了,我使用以下条件:

Intent intent = new Intent( context, AlarmReceiver.class );
boolean alarmUp = (
PendingIntent.getBroadcast( context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null)

但这似乎效果不太好,这是知道闹钟是否响了的好方法吗?

提前致谢

I want to set an alarm in my application which will be triggered each day. According to the doc, I have to set a one-time alarm, and in the BroadcastReceiver which will receive the alarm signal, reset the alarm for the day after.
Is that correct ?

My BroadcastReceiver handles well the wakelock and launch a service which releases this wakelock. Everything works fine here.

However I have problems. In my application there is a checkbox which is checked when alarm is up. To know if my alarm is up, I use the following condition :

Intent intent = new Intent( context, AlarmReceiver.class );
boolean alarmUp = (
PendingIntent.getBroadcast( context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null)

But this doesn't seem to work very well, is that a good way to know if an alarm is up ?

Thanks in advance

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

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

发布评论

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

评论(1

幸福丶如此 2024-08-26 02:35:24

对于问题的第一部分,您可以只使用重复警报,或者在像您一样触发警报时安排一个新警报。无论哪种方式都有效。

您可能还需要设置一个接收 ACTION_BOOT_COMPLETED 的广播接收器,以便您可以在手机重新启动时重新安排闹钟。

至于检查警报是否存在,带有 FLAG_NO_CREATE 的 PendingIntent 正是您的做法。

For the first part of your question, you could just use a repeating alarm, or schedule a new alarm whenever one fires like you are doing. Either way works.

You may also want to setup a broadcast receiver that receives ACTION_BOOT_COMPLETED so you can reschedule your alarms when the phone reboots.

As for checking if the alarm exists, the PendingIntent with FLAG_NO_CREATE is exactly how you would do that.

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