无法使用警报管理器取消待定意图

Not able to cancel Pending Intent using Alarm manager

这是设置和删除通知的代码。如果您需要更多详细信息,请告诉我。堆栈溢出的唯一解决方案是关于相同的未决意图。我已经尝试过该解决方案,但没有用。

public void setAlarm () throws java.text.ParseException {
    RealmResults<Reminder> realmResults = getAllLatest();

    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Reminder reminder =  getLatestReminder(realmResults);
    int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
    int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
    PendingIntent pendingIntent  = getPendingIntentFromReminder(reminder);
    Log.v("Reminder is: ", String.valueOf(reminder));
    Calendar calendar = Calendar.getInstance();
    //calendar.setTimeZone(TimeZone.getTimeZone());
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);


    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);
}
private PendingIntent getPendingIntentFromReminder(Reminder reminder) throws ParseException {
    int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
    int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
    Intent intent = new Intent(this, MyReceiver.class);
    intent.setAction(reminder.getName());
    Uri data = Uri.withAppendedPath(Uri.parse("TYS"),
            String.valueOf(reminder.getId()));
    intent.setData(data);
    Calendar calendar = Calendar.getInstance();
    //calendar.setTimeZone(TimeZone.getTimeZone());
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);
    //Log.v("Test", calendar.getTimeZone().toString());
    // Log.v("Hours to show", String.valueOf(hour));
    // Log.v("Minutes to show", String.valueOf(minutes));
    intent.putExtra("reminderTitle", reminder.getName());
    //  Log.v("Reminder Name", reminder.getName());
    intent.putExtra("notificationType", 1);
    intent.putExtra("reminderId", String.valueOf(reminder.getId()));
    intent.putExtra("reminderSubtitle", reminder.getSubTitle());
    intent.setAction(Long.toString(System.currentTimeMillis()));
    Log.v("set Alarm", String.valueOf(intent));
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    return alarmIntent;
}

public void deleteReminderIntent(Reminder reminder) throws ParseException {
    Log.v("deleteReminderIntent", String.valueOf(reminder));
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent =getPendingIntentFromReminder(reminder);
    alarmMgr.cancel(pendingIntent);
    pendingIntent.cancel();
    //alarmMgr.cancel(temp);
   // alarmMgr.cancel(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}

getPendingIntentFromReminder() 中,您将 Intent 中的 ACTION 设置为当前时间。当你调用方法获取PendingIntent来取消你的闹钟时,当前时间不同,所以ACTION不会匹配你习惯的PendingIntent设置闹钟。

为了取消闹钟,您需要使用与您用来设置闹钟的 Intent 匹配的 IntentIntent 中的以下参数必须匹配:

  • 操作
  • 类别
  • 数据
  • 组件

Intent 中的任何 "extras" 都将被忽略,因此它们不需要匹配。

有关详细信息,请参阅 Is it possible to create multiple PendingIntents with the same requestCode and different extras?