AlarmManager无法取消警报

时间:2015-04-03 04:24:09

标签: android notifications alarmmanager android-pendingintent

让我们看看是否有人发现了这个问题并且可以帮助我解决这个问题。我有以下情况。该应用程序是一个简单的待办事项列表:

public class NewToDo extends Activity {

    private PendingIntent pendingIntent; 
    private AlarmManager manager;
  ...

当用户创建带有通知的待办事项时,我会在此类的一个方法中创建它,如下所示:

  int interval = 30000;
  Intent alarmIntent = new Intent(this, AlarmReceiver.class);
  /*Let's let the putExtras apart, as documentation says they're not considered when comparing intents, but I add some of them*/
  manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, alarmIntent, alarmIntent.FILL_IN_DATA);
  /*Printing _id we see 3, for instance, but is a unique identifier to associate the notification with this to-do item*/
  /*I use the flag alarmIntent.FILL_IN_DATA because I want to send extras to create a custom notification*/                  
  manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + interval, interval,
                pendingIntent);

我退出了应用程序(但是如果我不退出则没有区别)。然后每30秒正确到达通知,并选择它,应用程序将在项目的详细视图上启动。操作栏按钮允许编辑它,再次调用同一个类NewToDo。 在编辑时,我可以取消与待办事项相关的通知,而我的代码就像在NewToDo类中那样 - :

  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent i = new Intent(this, AlarmReceiver.class); //creating a matching Intent

  Log.v(getString(R.string.app_name), "_id: " + _id);  //the 3 aforementioned

  PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), _id, i, alarmIntent.FILL_IN_DATA);
  alarmManager.cancel(displayIntent);

但是通知每30秒就会出现一次,因此它们不会被取消。

我在另一篇文章中看到了以下建议:

  

“Intent intentYouWantToCancel = new Intent(this,AlarmReceive.class);   intentYouWantToCancel.setAction( “aRelevantString”);

     

设置闹钟管理器以及何时删除它。 “

所以我把两个意图都作为'aRelevantString'投入到唯一标识符的String中。但同样的问题。

同样改变了 this 的getBaseContext(),以防万一,但没有区别。

没有错误,只是通知不断重复忽略我的取消。任何的想法?

PS:我每次运行它都会重新启动AVD。我意识到我使用filterEquals的打印方式完全没有任何意义(这个时间太多了),但这并不会影响问题的其余部分。

2 个答案:

答案 0 :(得分:0)

如下所示修改您的代码并尝试,这可能会对您有所帮助。这是我之前从SO本身使用的那个:

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);

答案 1 :(得分:0)

我终于解决了这个问题。要创建警报:

public class NewToDo extends Activity {

    private PendingIntent pendingIntent; 
    private AlarmManager manager;
    ...

int interval = 30000;
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("id", _id);
pendingIntent = PendingIntent.getBroadcast(this, _id, alarmIntent,0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + interval, interval,
                pendingIntent);

取消具体警报:

manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), _id, i, 0);

manager.cancel(pendingIntent);
pendingIntent.cancel();