如何清除报警管理器中的先前报警?

时间:2012-02-15 09:28:38

标签: android alarmmanager alarm android-alarms

我正在开发一个警报应用程序,因为我正在使用警报管理器概念,因为用户可以选择否。提前几天警报(如果他选择5天警报将在5天之前到来)比较天数列表来自sqlite数据库。我写了一些代码,其工作问题是当用户更改警报剩余天数时(例如, 3,而不是5)设置前(5天)报警不清楚共有8个报警(5 + 3)请任何人解决我的问题。

dbcal.set(currentcal.get(Calendar.YEAR), mnth,dt);
Log.e("dbCal Alarm","first"+dbcal.getTime());
if(!dbcal.getTime().before(currentcal.getTime())){
    uniqueno++;
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(SampledateActivity.this,AlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(SampledateActivity.this, uniqueno, intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, dbcal.getTimeInMillis(), pi);
}
for(int k = 1 ; k <= time ;k++){
   Log.e("Entered to alarm","yes");
   dbcal.add(Calendar.HOUR, -24);
   if(!dbcal.getTime().before(currentcal.getTime())){
        uniqueno++;
        Intent intentlocal = new Intent(SampledateActivity.this,AlarmReceiver.class);
        PendingIntent pilocal = PendingIntent.getBroadcast(SampledateActivity.this, uniqueno, intentlocal, 0);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, dbcal.getTimeInMillis(), pilocal);
        Log.e("dbCal Alarm","k: "+"k value:"+k+(time*i+k)+":"+dbcal.getTime());
  }
}

2 个答案:

答案 0 :(得分:8)

使用您用于创建PendingIntent的{​​{1}}创建requestCode PendingInent来启动Alarm

Intent intentlocal = new Intent(SampledateActivity.this,AlarmReceiver.class);
PendingIntent pilocal = PendingIntent.getBroadcast(SampledateActivity.this,
                                                       uniqueno, intentlocal, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pilocal);
pilocal.cancel();

答案 1 :(得分:0)

我得到了解决问题的方法。每次我刷新我的活动。即...当我点击设置的闹钟按钮时,我已经刷新了我的活动。所以在删除设置警报之前,会创建新输入的警报。