如何处理android中每天触发的闹钟
我想在我的应用程序中设置一个每天触发的闹钟。根据文档,我必须设置一次性闹钟,并在将接收闹钟信号的广播接收器中,重置后一天的闹钟。 这是正确的吗?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于问题的第一部分,您可以只使用重复警报,或者在像您一样触发警报时安排一个新警报。无论哪种方式都有效。
您可能还需要设置一个接收 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.