I am trying to create an alarm application. I am getting the alarm time settings from the user using a time picker
I have studied the following comments but could not find a solution:
Alarm starts at the wrong time
AlarmManager fire at the wrong time
AlarmManager running at the wrong time
When I set the alarm time before the current time, it starts ringing immediately. However, if the alarm time is set after the current time, it works fine.
Following is my code...
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getHour(),
timePicker.getMinute(),
0
);
AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,0,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
If the user sets the alarm after the current time, it works fine. However, if the time is set before the current time (for the upcoming day), the ring immediately starts.
Kindly help me resolve this. Thanks in advance!