Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager fires alarms in the past immediately before BroadcastReceiver can reschedule it

I have a BroadcastReceiver which reschedules alarms on events such as booting and time change. But when the time is past the trigger time of the alarm (For example, when the user manually changes the time from the settings), AlarmManager fires the alarm immediately before I can add a day to reschedule my alarm. How can I avoid this?

I'm currently using the set and add methods of the Calendar to schedule the alarms.

        for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; dayOfWeek++) {
            if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek >= nowDay &&
                    !(dayOfWeek == nowDay && alarm.timeHour < nowHour) &&
                    !(dayOfWeek == nowDay && alarm.timeHour == nowHour && alarm.timeMinute <= nowMinute)) {

                calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
                alarmSet = true;
                break;
            }
        }

        if (!alarmSet) {
            for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; dayOfWeek++) {
                if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek <= nowDay) {
                    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
                    calendar.add(Calendar.WEEK_OF_YEAR, 1);
                    break;
                }
            }
        }

It is also stated in the docs:

If the stated trigger time is in the past, the alarm will be triggered immediately.

How can this behaviour be altered?

like image 233
Piyush Avatar asked Jan 06 '16 22:01

Piyush


People also ask

How does an application get access to the AlarmManager?

How does an application get access to the AlarmManager? Use the AlarmManager() constructor to create an instance of the AlarmManager. Use the AlarmManager. newInstance() method to retrieve the singleton instance of the AlarmManager.

How do you set a repeat alarm on Android?

At the top of the main panel you should see an option to Add alarm. Tap this and you'll be presented with a time in the top half of the screen, with various settings in the lower half. Scroll the hours up or down until you reach the one you want, then repeat the process with the minutes.

How do I use alarm Manager?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

This is intended. See e.g. Set a Repeating Alarm

A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.

or the same here

If the stated trigger time is in the past, the alarm will be triggered immediately.

To avoid this you will have to either manually check your alarms before adding them,

if(alarmTimeStamp < System.currentTimeMillis()) {
    // if is repeating schedule in __interval__
    // else ignore
}

or ignore them in your receiver if the date is in the past.

like image 81
David Medenjak Avatar answered Nov 15 '22 17:11

David Medenjak