即使被另一个 activity 取消,警报通知也会继续触发

Alarm notification keeps firing even if it is canceled from another activity

我从 activity A 设置了闹钟,然后在 activity B 我可以删除和更新 activity A 中设置的闹钟。闹钟更新有效,但删除无效,我已经从这里尝试了一些答案,但似乎没有任何效果。

我的闹钟是这样设置的。

public void Alarms() {
    for(eventlist_model model:arrayList)
    {
        int id=model.getEvent_id();
        String type=model.getEvent_type();
        String date=model.getEvent_date();
        String time=model.getEvent_time();
        String note=model.getEvent_note();
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try{
            String sched=date+" "+time;
            Date newSched=format.parse(sched);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(newSched);
            if (calendar1.before(calendar))
                calendar1.add(Calendar.DATE, 1);
            Intent intent = new Intent(this, events_receiver.class).setAction("Dog_alarm");
            intent.putExtra("type", type);
            intent.putExtra("note", note);
            intent.putExtra("id", id);
            PendingIntent dog1 = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            final AlarmManager alarm1 = (AlarmManager) getSystemService(ALARM_SERVICE);
            if (Build.VERSION.SDK_INT >= 19) {
                alarm1.setExact(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), dog1);
            } else {
                alarm1.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), dog1);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        }

}

然后在activity B 我用这个取消闹钟

public void cancelTask()
{   int id=Integer.valueOf(eventid);
    Intent intent = new Intent(update_task.this, events_receiver.class);
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent task = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarm.cancel(task);
}

但它不起作用,我被困在这里。谁能帮我解决这个问题?

根据 cancel AlarmManager 方法的文档:

void cancel (PendingIntent operation)

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

表示如果检测到intentA等于intentB,使用filterEqauls方法,即intentA.filterEquals(intentB)==true,那么可以使用alarmanger中的intentB取消已在 alarmManager 中设置的 intentAfilterEquals 文档通知:

boolean filterEquals (Intent other)

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

这意味着它 filterEquals 不关心额外的内部意图来比较它们,但其他参数如 Action 应该 完全 相同。 在您的情况下,您错过了在取消意图中调用 setAction

所以你应该像这样改变你的cancelTask方法:

public void cancelTask()
{   int id=Integer.valueOf(eventid);
    Intent intent = new Intent(update_task.this, events_receiver.class);
    intent.setAction("Dog_alarm");//<--- this is missed
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    PendingIntent task = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarm.cancel(task);
}