Android Prevent Immediate Trigger of Alarm Service If Alarm Time Has Passed For the Day

android prevent immediate trigger of alarm service if alarm time has passed for the day

You don't need to create Timestamps. You can do it with your Calendar.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);

if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}

alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);

I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

AlarmManager alarm is called immediately when trigger time is in the past

This is the expected behavior.

From the documentation of setRepeating() (and other AlarmManager set methods):

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

If you would like to prevent that happening, then simply do not set alarms with a past trigger time (e.g. check against System.currentTimeMillis() when setting the alarm).

How to prevent alarm from running the passed time on starting

I suggest you to use to calendar.add(Calendar.DAY_OF_YEAR, 1);

Study following code:

Calendar calendar = Calendar.getInstance();
// 9 AM
calendar.add(Calendar.DAY_OF_YEAR, 1); ///to avoid firing the alarm immediately
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);

Original Source

How to Prevent Past Alarms Going of Immediately

The Android alarm service will trigger every alarm that his programmed time is less than the current time (i.e. it will trigger immediately the alarms that are late). The method "getInstance" of the Calendar class returns you a calendar instance that has the current time. If you are only setting the hours, minutes and seconds of the calendar instance, then it will use the current day, so the alarm will be trigger immediately because the time has just passed. If you want to program for tomorrow an alarm if the selected time is less than the current, then you will need to verify if the selected hour is less than the current and conditionally add one day to the calendar (or sum to the millis result "24*60*60*1000" which is a day in milliseconds). Hope this helps.

Android Alarm firing instantly when I set alarm on earlier hour or after midnight

Of course it does... It's meant to work so.
Android recognizes that the time is past, so it will fire the alarm, even if it's late.

You can make sure that the time set for the alarm is after the current time. Just calculate this difference:

int diff = Calendar.getInstance().getTimeInMilis() - targetCal.getTimeInMillis();

If diff is greater than 0, then add a day to your calendar (targetCal)
Now, your device's time will be earlier (instead of being later) than the next scheduled alarm time.


Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);

if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}

alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);


Related Topics



Leave a reply



Submit