取消Android报警/通知

问题描述:

我有一个应用程序,通知您,如果您有任何支付账单。我希望每个帐单都有一个通知。警报设置为在当天的确切时间熄灭。取消Android报警/通知

我除了一个方面一切工作:

报警消失后,我刷出来通知面板。然后,当我进入应用程序时,我总是检查重置所有警报(如果用户更改日期),但同一个警报一遍又一遍地重新出现。即使已经过去了。

这是我设置闹钟的代码。我环路直通每个账单,看看是否报警(该月的一天)设置:

public static void setRepeatingAlarm(Context c) { 

     List<Debt> debts = datasource.getAllDebt(); 

     int count = 0; 

     for (Debt d : debts) { 

      int i; 
      if (String.valueOf(d.getDay()) == null) { 
       i = 300; 
      } else { 
       i = Integer.valueOf(d.getDay()); 
      } 

      Calendar cal = Calendar.getInstance(); 
      cal.set(Calendar.DAY_OF_MONTH, i); 
      cal.set(Calendar.HOUR_OF_DAY, 12); 
      cal.set(Calendar.MINUTE, 22); 
      cal.set(Calendar.SECOND, 10); 

      Intent intent = new Intent(c, TimeAlarm.class); 
      Bundle b = new Bundle(); 
      b.putString("keyvalue", d.getName()); 
      b.putInt("id", (int) d.getId()); 
      intent.putExtras(b); 

      pendingIntent = PendingIntent.getBroadcast(c, (int) d.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
     } 
    } 

这里就是我所说的通知:

public class TimeAlarm extends BroadcastReceiver { 


    @Override 
    public void onReceive(Context context, Intent intent) { 

     String debtName = intent.getStringExtra("keyvalue"); 
     int id = intent.getIntExtra("id", 0); 
     CharSequence message = "Click to Update Balance for " + debtName; 

     Intent notificationIntent = new Intent(context, MainFragmentActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, id, notificationIntent, 0); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.ic_notify) 
         .setContentTitle("Payment Due") 
         .setContentText(message) 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(message)); 

     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setAutoCancel(true); 

     NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     nm.notify(0, mBuilder.build()); 


    } 
} 

有人可以帮助我这些问题?

+0

嘿!我有同样的问题,已经解雇的警报一次又一次地被解雇。你能告诉我你是如何取消警报的吗?我看到了你的答案,但我需要从你那里知道。 – Apurva 2015-02-21 11:09:50

+0

以下是正确的。只要确保您在待决意图中使用相同的专利权人,就像您在设置时一样。 – KickingLettuce 2015-02-21 13:02:36

+0

好吧我会试试 – Apurva 2015-02-21 16:40:32

您可以通过将意图传递给alarmManager.cancel(intent)来显式地取消警报;

+0

我已经看到了这个,但不确定在哪里叫它。我假设不在接收器中。 – KickingLettuce 2015-02-11 21:15:58