如何让通知管理器不获取过去的时间?
How to make the notification manager not get the times in past?
我不知道标题是否清楚,我的通知管理器问题是当我将时间设置为 ( 1:00 , 2:00 , 3:00 , 4:00) 现在时间是 2:30,正常的通知会等到 3:00 然后 4:00,但是在我的代码中,通知管理器给我两个警告 1:00 和 2:00 因为它们是过去的,然后等到 3:00 并通知然后 4:00 并通知,所以我的问题是过去的时间我不想得到通知他们。
我的代码
Main class.java , alertTimes 是一个以毫秒为单位的数组
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);
我也在 AlertReceiver.java 中尝试这样做,但这也行不通
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);
您应该仅将警报安排在未来的时间,因为如果指定的触发时间已过去,set()
将立即触发警报。
请参阅下面的文档:
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.
所以,要解决你的问题,请检查你需要设置的闹钟时间是否在未来,然后只设置它,否则忽略,
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));
}
}
我不知道标题是否清楚,我的通知管理器问题是当我将时间设置为 ( 1:00 , 2:00 , 3:00 , 4:00) 现在时间是 2:30,正常的通知会等到 3:00 然后 4:00,但是在我的代码中,通知管理器给我两个警告 1:00 和 2:00 因为它们是过去的,然后等到 3:00 并通知然后 4:00 并通知,所以我的问题是过去的时间我不想得到通知他们。
我的代码 Main class.java , alertTimes 是一个以毫秒为单位的数组
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);
我也在 AlertReceiver.java 中尝试这样做,但这也行不通
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);
您应该仅将警报安排在未来的时间,因为如果指定的触发时间已过去,set()
将立即触发警报。
请参阅下面的文档:
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.
所以,要解决你的问题,请检查你需要设置的闹钟时间是否在未来,然后只设置它,否则忽略,
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));
}
}