The reference for Alarm Manager says that
If the stated trigger time is in the past, the alarm will be triggered
immediately.
I am facing this problem in my application. Here is my alarm manager code :
Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
Is there any workaround to this problem?
-----EDIT------
I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :
Calendar calendar = Calendar.getInstance();
long currentTime = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
long setTime = calendar.getTimeInMillis();
Timestamp setTimestamp = new Timestamp(setTime);
Timestamp currentTimestamp = new Timestamp(currentTime);
if (setTimestamp.after(currentTimestamp))
{
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
}
else
{
}
What should I the alarmManager
to in case setTimestamp
is before currentTimestamp
?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…