Jonny Jonny - 4 months ago 14
Java Question

AlarmManager only works for AM hours

I would like to know, why my alarmanager only works when I setup the hour in

"AM"
in the timepicker. Even if it is 7:00PM (and so it is on the phone), it will not work if I set the alarm to PM as it should be for that time, but it will work if I set the alarm for the
"AM"
hour.

As you can see, all
"AM"
hours works well, but when i try to setup the alarm for
"PM"
it does not work at all.

Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.set(Calendar.MONTH, month);
alarmCalendar.set(Calendar.HOUR, hour);
alarmCalendar.set(Calendar.MINUTE, minute);

Intent alarm_on = new Intent(this, MainTaskAlarmReciever.class);

alarm_on.putExtra("extra", "alarm on");

pendingIntent = PendingIntent.getBroadcast(this, 0, alarm_on, PendingIntent.FLAG_UPDATE_CURRENT);

mAlarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);

Answer

You should use Calendar.HOUR_OF_DAY

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

Calendar alarmCalendar = Calendar.getInstance();
    alarmCalendar.set(Calendar.MONTH, month);
    alarmCalendar.set(Calendar.HOUR_OF_DAY, hour);
    alarmCalendar.set(Calendar.MINUTE, minute);

    Intent alarm_on = new Intent(this, MainTaskAlarmReciever.class);

    alarm_on.putExtra("extra", "alarm on");

    pendingIntent = PendingIntent.getBroadcast(this, 0, alarm_on, PendingIntent.FLAG_UPDATE_CURRENT);

    mAlarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);