如何一次取消具有重复的PendingIntent的多个警报? [英] How to cancel multiple alarms with duplicate PendingIntent at once?
问题描述
我正在设置多个警报,它们将具有完全相同的 PendingIntent
对象,但是它们关闭的时间不同。如果我通过向 PendingIntent
提供相同的 Intent
对象来取消一个警报,并将其传递给 AlarmManager.cancel()
,它会取消所有警报(即具有不同的开始时间)还是会表现出其他行为?
I am setting multiple alarms which will have the exact same PendingIntent
object, but their time of going off is different. If I cancel one alarm by providing the same Intent
object to PendingIntent
, and pass this to AlarmManager.cancel()
, will it cancel all the alarms (i.e. with different starting times) or will it exhibit some other behavior?
这是代码:
while(size != 0)
{
timeToSet = getNewTime(); //gets calendar object
Intent intent = new Intent(this, MyAlarmReceiver.class);
intent.putExtra("uniqueid", uuid);
intent.putExtra("vibrate", myalarm.getVibrate());
intent.putExtra("important", myalarm.getImportant());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToSet.getTimeInMillis(), 604800000, pendingIntent);
size--;
}
因此,所有警报的Intent对象均相同,但时间不同。
现在,当我取消这些警报时:
So the Intent object is same for all the alarms, but the time is different. Now when I cancel these alarms:
Intent intent = new Intent(this, MyAlarmReceiver.class);
intent.putExtra("uniqueid", uuid);
intent.putExtra("vibrate", myalarm.getVibrate());
intent.putExtra("important", myalarm.getImportant());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
是否将取消所有警报?那就是我想要的。我可以在每个 PendingIntent
中使用不同的 requestCode
,但是如果所有这些都被取消(因为我不这样做)会更好想要维护和存储这些额外的 requestCodes
。
Will all the alarms get cancelled? That is what I want it to do. I could use a different requestCode
in every PendingIntent
, but it will be better if all of them get cancelled as I dont want to maintain and store these extra requestCodes
.
谢谢!
推荐答案
如果您有预定义数量的警报,则可以设置唯一的 requestCode
(第二个参数 getBroadcast
)。但我不确定是否适合您。如果您未指定 requestCode
,则所有警报将与 filterEquals
视为相等,因此将取消所有警报
If you have a predefined amount of alarms you can set a unique requestCode
(second parameter of getBroadcast
) for each alarm. But I'm not sure if that works for you. If you don't specify a requestCode
all alarms will be seen as equal by filterEquals
and therefore all alarms will be canceled.
这篇关于如何一次取消具有重复的PendingIntent的多个警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!