如何取消AlarmManager

时间:2013-05-14 06:28:13

标签: android alarmmanager

我按如下方式设置了alarmManager:

    Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);

    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

以下应该取消它,但它总是失败。我错过了什么?

 AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, start);
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength);

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

我已经在StackOverflow上阅读了很多答案,但仍然无法弄清楚问题是什么 提前谢谢!

3 个答案:

答案 0 :(得分:2)

要让AlarmManager取消某个事件,需要比较两个意图。

这是通过说明

的filterEquals(Intent other)方法实现的
  

为了意图的目的,确定两个意图是否相同   分辨率(过滤)。也就是说,如果他们的行动,数据,类型,类,   和类别是一样的。这不会比较任何额外的数据   包含在意图中。

我不确定为什么它在课程相同时不匹配。

我也有同样的问题,要克服我已经设置了数据。

看看这有助于你。

<强> EDITED

设置闹钟时

    Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);
    intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 
    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

需要取消时

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, start);
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength);
intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

答案 1 :(得分:2)

我会在这里发布我的答案,希望能帮助他人节省一些时间 以某种方式在两边添加标志PendingIntent.FLAG_UPDATE_CURRENT解决了这个问题。

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

解决了这个问题 设置闹钟

 Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);

    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

取消闹钟

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

答案 2 :(得分:0)

您需要创建待处理的意图,然后取消它

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);

// Cancel alarms
try {
    alarmManager.cancel(pendingUpdateIntent);
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}