开发者

How to handle an alarm triggered each day in android

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 h开发者_Python百科andles 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜