Android:AlarmManager 无法取消警报
我正在使用 AlarmManager 在我的应用程序中设置多个警报,但遇到了问题。当以这种方式设置 PendingIntent 时:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, 0, alarmIntent, 0);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
当 requestCode 设置为 0(在 getBroadcast 中的上下文旁边)时,我可以使用:
myAM.cancel(timerAlarmIntent);
取消我在应用程序中设置的任何数量的警报。然而,我设置的每个闹钟都会覆盖上一个闹钟,因此最终只有一个闹钟响起。如果我将 requestCode 设置为唯一的数字,例如:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
其中 i 使用 for 循环设置为每个警报的唯一 ID 号。这样,所有警报都可以正常运行,但我无法取消警报,因为当我运行时,只有一个警报实际上会取消:
myAM.cancel(timerAlarmIntent);
How do I set the requestCode to a unique number and have the option to cancel it?
我最终用来解决问题的代码是:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
intentArray.add(timerAlarmIntent);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
要取消 PendingIntents,我设置了以下方法:
private void cancelAlarms(){
if(intentArray.size()>0){
for(int i=0; i<intentArray.size(); i++){
myAM.cancel(intentArray.get(i));
}
intentArray.clear();
}
}
I am using the alarmManager to set up multiple alarms within my application and have run into an issue. When setting the PendingIntent this way:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, 0, alarmIntent, 0);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
Where the requestCode is set to 0 (beside the context in getBroadcast) I can use:
myAM.cancel(timerAlarmIntent);
to cancel any amount of alarms I set in the application. However, each alarm I set overwrites the last so only one alarm ends up going off. If I set the requestCode to a unique number such as:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
Where i is set to a unique id number for each alarm using a for loop. This way all alarms run fine, but I cannot cancel the alarms because only one alarm will actually cancel when I run:
myAM.cancel(timerAlarmIntent);
How do I set the requestCode to a unique number and also have the option to cancel it?
The code I ended up using to solve the problem is:
timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
intentArray.add(timerAlarmIntent);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);
To cancel the PendingIntents I set up the following method:
private void cancelAlarms(){
if(intentArray.size()>0){
for(int i=0; i<intentArray.size(); i++){
myAM.cancel(intentArray.get(i));
}
intentArray.clear();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将timerAlarmIntent对象存储在列表中并在需要时在循环中取消它们?
问候,
史蒂芬
Why don't you store timerAlarmIntent objects in a list and cancel them in a loop when needed ?
Regards,
Stéphane