Search code examples
javaandroidcalendaralarmmanager

Setting a time in calendar in the same day moves to the next day


I'm displaying a TimePicker and I want an Alarm to get triggered on that time in the same day, but weirdly, it's getting called a day after, unless the hour the user picks has already passed, in that case the Alarm triggers immediately, can you help me figuring out what's happening?

startingHour = timePickerStartingHour.getCurrentHour();
startingMinute = timePickerStartingHour.getCurrentMinute();

Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, startingMinute);
cal.set(Calendar.HOUR, startingHour);

    Intent intent = new Intent(getActivity(), NotificationBroadcast.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 100000+i, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);

Solution

  • If anyone bumps into this issue, replace Calendar.HOUR with Calendar.HOUR_OF_DAY, it worked for me, props for mgcaguioa