Android:取消从另一个活动设置的闹钟

发布于 2024-11-29 13:41:11 字数 408 浏览 2 评论 0原文

我将尽力解释这一点。基本上,我的活动 1 使用外部类来执行各种操作。 Activity 2 还引用 Activity 1 的所述ExternalClass 对象。在这两个活动中,我可以使用 AlarmManager 设置警报,但我希望能够取消从任一活动(即活动 1)创建的所有警报。

所有警报均使用相同的意图和相同的 AlarmManger 设置(均在ExternalClass),但是当我单击 Activity 1 中应该调用 myAlarms.cancel(intent) 的按钮时,它只会取消使用 Activity 1 类创建的警报。

在活动 2 中,ExternalClass 是通过引用活动 1 中创建的类的对象来引用的,因此它们都应该使用相同的ExternalClass 实例。我很确定它不会取消警报,因为设置警报时使用的上下文,但我不知道如何解决这个问题。

I will try to explain this as best as I can. Basically, I have Activity 1 that uses an ExternalClass to do various things. Activity 2 also references Activity 1's object of said ExternalClass. From both of these activities I can set alarms using the AlarmManager, but I want to be able to cancel all the alarms created from either activity, from Activity 1.

All alarms are set using the same intent and the same AlarmManger (both created in the ExternalClass), but when I click my button in Activity 1 that is supposed to call myAlarms.cancel(intent) it only cancels the alarms that were created using the Activity 1 class.

The ExternalClass is referenced in Activity 2 by referencing the object of that class that was created in Activity 1 so they should both be using the same instance of the ExternalClass. I'm pretty sure it isn't canceling the alarms because of the context that was used when setting the alarms, but I can't figure out how to get around that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

生来就爱笑 2024-12-06 13:41:11

为了解决这个问题,我使用了以下代码:

timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
intentArray.add(timerAlarmIntent);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);

我将 requestCode 设置为唯一的 id。这是在 for 循环中,i 代表 0、1、2...

要取消警报,我必须将每个警报添加到列表中,并在想要取消所有警报时循环遍历该列表。

private void cancelAlarms(){
if(intentArray.size()>0){
    for(int i=0; i<intentArray.size(); i++){
        myAM.cancel(intentArray.get(i));
    }
    intentArray.clear();
}

}

To solve this issue I used to following code:

timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
intentArray.add(timerAlarmIntent);
myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);

I set the requestCode to a unique id. This is within a for loop and i represents 0, 1, 2...

To cancel the alarms I had to add each alarm to a list and loop through the list when I wanted to cancel all alarms.

private void cancelAlarms(){
if(intentArray.size()>0){
    for(int i=0; i<intentArray.size(); i++){
        myAM.cancel(intentArray.get(i));
    }
    intentArray.clear();
}

}

杯别 2024-12-06 13:41:11

要取消并发出警报,您需要将等效的 PendingIntent(即 p1.equals(p2) 返回 true)传递给用于创建它的对象。从哪里创建 AlarmManager 引用并不重要。在这两种情况下,您如何初始化 PendingIntent

如果两个 PendingIntents 都表示相同的操作,则认为它们相等
相同的包。基本上,如果您用等效的 Intents 初始化两个 PendingIntents,它们将被视为相等。编辑:文档对此显然是错误的,在比较 PendingIntents 时也使用了 requestCode。查看评论和其他答案。

To cancel and alarm you need to pass an equivalent PendingIntent (meaning p1.equals(p2) returns true) to the one used to create it. It doesn't matter from where you created the AlarmManager reference. How are you initializing the PendingIntent in both cases?

Two PendingIntents are considered equal if they both represent the same operation from the
same package. Basically if you initialize two PendingIntents with equivalent Intents, they would be considered equal. EDIT: the documentation is obviously wrong about this, the requestCode is also used when comparing PendingIntents. See comments and other answer.

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