Why does my AlarmManager fire instantly?

I am trying to create an alarm application. I used to have an alarm and I could set different times and the alarm would go off accordingly. Then I changed the layout of ChangeAlarmActivity to TableLayout and now it doesn't work? I haven't touched the code. This is how I set the alarm:

Intent alarmIntent = new Intent(ChangeAlarmActivity.this, AlarmReceiver.class);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(ChangeAlarmActivity.this, (int)alarm.getID(),
        alarmIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
System.out.println("Alarm time: " + hour + ":" + min);
Calendar alarmCal = Calendar.getInstance();
//alarmCal.setTimeInMillis(System.currentTimeMillis());
alarmCal.set(Calendar.HOUR_OF_DAY, hour);  
alarmCal.set(Calendar.MINUTE, min);        
//alarmCal.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP,
        alarmCal.getTimeInMillis(),
        pendingAlarmIntent);

      

+2


source to share


1 answer


Perhaps because your hour and minute is in the past.

Let's say that in your current timezone it is 4:07 pm (using 24-hour time) and you run this code with hour

how 3

and min

how 27

. 03:27 was in the past and therefore the alarm goes off immediately.



After your two calls, set()

see if the time has passed before, and if so, add()

one day.

+15


source







All Articles