I am using alarm manager to assign the alarm at specific time. I encountered a problem which is if I set the time earlier than the current system time, then the alarm action will be fired instantly. How to prevent this behavior ? Thanks
My code:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 234324243, serviceIntent, 0);
/*sample of setting eariler time
timeAlarm.set(Calendar.YEAR, 2013);
timeAlarm.set(Calendar.MONTH, 10);
timeAlarm.set(Calendar.DAY_OF_MONTH, 13);
timeAlarm.set(Calendar.HOUR_OF_DAY, 12);
timeAlarm.set(Calendar.MINUTE, 9);
timeAlarm.set(Calendar.SECOND, 0);
*/
alarmManager.set(AlarmManager.RTC_WAKEUP, timeAlarm.getTimeInMillis(), pendingIntent);
This is because the android alarm manager is designed by this. You can modify your time if the time earlier than the current system time by 1 day or more. like this:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
if (c.getTimeInMillis() < System.currentTimeMillis()){
c.add(Calendar.DAY_OF_MONTH, 1);
}