Android:AlarmManager 无法取消警报

发布于 2024-11-29 11:42:40 字数 1429 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-12-06 11:42:40

为什么不将timerAlarmIntent对象存储在列表中并在需要时在循环中取消它们?

问候,
史蒂芬

Why don't you store timerAlarmIntent objects in a list and cancel them in a loop when needed ?

Regards,
Stéphane

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文