1

我已经设置了一个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 上阅读了很多答案,但仍然无法弄清楚问题所在。
提前致谢!

4

3 回答 3

3

AlarmManager 要取消一个事件,需要比较两个 Intent。

这是通过 filterEquals (Intent other) 方法实现的,该方法说

出于意图解析(过滤)的目的,确定两个意图是否相同。也就是说,如果它们的动作、数据、类型、类和类别相同。这不会比较意图中包含的任何额外数据。

我不确定为什么当班级相同时它不匹配。

我也有同样的问题,为了克服我也设置了一个数据。

看看这是否对你有帮助。

已编辑

设置闹钟时

    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());
}
于 2013-05-14T07:52:21.727 回答
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());
}
于 2013-05-14T10:32:46.253 回答
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());
}
于 2013-05-14T06:36:24.277 回答