避免警报重复在android中

时间:2013-04-08 12:15:10

标签: android android-alarms

嗨,我是新手Android开发我正在开发一个与notification报警相关的应用程序,而我在特定时间设置报警,所有工作都完美正常并且报警正确,但报警notification重复有些时候,我不重复它,我怎么能避免这个问题。

这是我的代码:

Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("ti", name);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);      
final Calendar cal = Calendar.getInstance();        
switch(Cun.valueOf(repeat)) {
case No:        
 alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
 break;
}

1 个答案:

答案 0 :(得分:0)

警报首次通知后,使用以下方法取消:

Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("ti", name);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarmManager.cancel ( pendingIntent );