It say in this section of Android dev doc on Scheduling Repeating Alarms that "if trigger time you specify is in the past, the alarm triggers immediately". So if right now is 9am, and I set the AlarmManager to execute my daily notification at 8am, it won't wait until tomorrow at 8am to execute the notification, it will immediately pop up. How do I make it not pop up?
Here is my commented code:
/**
* Show alertDialog to pick time
* @param v
*/
@Override
public void onClick(View v){
Calendar c=Calendar.getInstance();
mHour=c.get(Calendar.HOUR_OF_DAY);
mMinute=c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog=new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int
minute) {
timeSet.setText(hour+":"+minute);
//storing the inputted hour and time, so they reappear when
//app reopens
editor.putInt("Hour",hour);
editor.putInt("Minute",minute);
editor.apply();
scheduleNotification(hour,minute);
}
},mHour,mMinute,true);
timePickerDialog.show();
}
/**
*Building Intent for AlarmReceiver and PendingIntent.
* Give PendingIntent to AlarmManager
* @param hour user-inputted hour of day
* @param minute user-inputted minute of day
*/
public void scheduleNotification(int hour, int minute){
Intent intent1=new Intent(this,AlarmReceiver.class);
//FLAG_UPDATE_CURRENT: if the described PendingIntent already exists,
//keep it but replace its extra data with what is in this new Intent.
PendingIntent pendingIntent=PendingIntent.getBroadcast(this
,0
,intent1
,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am=(AlarmManager)this.getSystemService(this.ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hour);
calendar.set(Calendar.MINUTE,minute);
//RTC_WAKEUP: wake up the device when it goes off.
am.setRepeating(AlarmManager.RTC_WAKEUP
, calendar.getTimeInMillis()
,AlarmManager.INTERVAL_DAY
,pendingIntent);
}
AlarmReceiver.class is for making the notification when it Receives the phone receives the pendingIntent and use it to call AlarmReceiver.class (to make notification).