I want to pick a time from time picker and set an alarm using AlarmManager for that time.
When I use when.getTimeInMillis
the value returned is different from the time I´ve set.
For example , if I set the time at 20:40 and I print the value of when.getTime
I get 17:12 from next day.
Why is this happening?
MedicationReminder mr=new MedicationReminder(getApplicationContext());
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
mr.setReminder(new Medication(name,quant_aux,time),c);
Intent i = new Intent(mContext, MedicationReceiver.class);
i.putExtra("medName",medication.getName());
i.putExtra("medQuant",medication.getQuantity());
PendingIntent pi=PendingIntent.getBroadcast(mContext,0,i,0);
mAlarmManager.set(AlarmManager.RTC_WAKEUP,when.getTimeInMillis(),pi);
Instead of
Calendar c=Calendar.getInstance();
c.add(Calendar.HOUR, hour);
c.add(Calendar.MINUTE, min);
Try doing
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR, hour);
c.set(Calendar.MINUTE, min);
If you want to use 24h hour format, then use
c.set(Calendar.HOUR_OF_DAY, hour);
I'm not 100% that's the problem since you didn't include all the code, but I think you're adding 20 hours to the current time instead of setting the calendar to hour 20.
Another note: I believe getCurrentHour() and getCurrentMinute() have been deprecated since API 23, so I suggest you use getHour() and getMinute() instead.