I don't know if the title clear or not, my problem with the notification manager is that when I set the times as ( 1:00 , 2:00 , 3:00 , 4:00) and the time now is 2:30, the normal notification will wait until 3:00 then 4:00 ,, but in my code the notification manager give me two alert for 1:00 and 2:00 because the are in past , then wait until 3:00 and notify then 4:00 and notify, so my problem is with the times in past I don't want to get notification for them.
my code Main class.java , alertTimes is an array that have the times in millisecond
for (int i = 0; i < alertTimes.length; i++) {
inte = new Intent(this, AlertMedicine.class);
inte.putExtra("id", i);
inte.putExtra("name", NameOftimes[i]);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTimes[i], PendingIntent.getBroadcast(this, 1, inte, PendingIntent.FLAG_UPDATE_CURRENT));
}
AlertReceiver.java
createNotification(context, "OK", "OK", "OK" + j, 1, 0);
I also try in AlertReceiver.java to do this but this also not work
if(calendar.get(Calendar.HOUR_OF_DAY)< Integer.parseInt(arr[id].substring(0, 2))) {
createNotification(context, "OK", "OK", "OK" + j, 1, 0);
}
else
if(calendar.get(Calendar.HOUR_OF_DAY)== Integer.parseInt(arr[id].substring(0, 2))){
int min=Integer.parseInt(arr[id].substring(3, 5))+2;
if(calendar.get(Calendar.MINUTE) <= min){
createNotification(context, "OK", "OK", "OK" + j, 1, 0);
You should schedule the alarms for future time only because set()
will trigger the alarm immediately if the stated trigger time is in the past.
See the doc below :
public void set (int type, long triggerAtMillis, PendingIntent operation) Added in API level 1
Schedule an alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler. If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled. If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
So, to fix your issue check if the alarm time you need to set is in future or not, then only set it otherwise ignore,
for (int i = 0; i < alertTimes.length; i++) {
if( alertTimes[i] >= System.currentTimeMillis()){
inte = new Intent(this, AlertMedicine.class);
inte.putExtra("id", i);
inte.putExtra("name", NameOftimes[i]);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTimes[i], PendingIntent.getBroadcast(this, i+1, inte, PendingIntent.FLAG_UPDATE_CURRENT));
}
}