gpt4 book ai didi

android - AlarmManager.setRepeating 是幂等的吗?

转载 作者:搜寻专家 更新时间:2023-11-01 09:18:33 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我设置了一个我想重复出现的闹钟,因此使用了 AlarmManager.setRepeating()。

我不想自己跟踪警报是否已设置(听起来是个坏主意,有时很容易失败),而且似乎没有 API 支持检查是否已设置特定警报对于给定的 Intent。

因此,每次我的应用程序激活时,我都会悲观地重置闹钟:

alarmManager.cancel(pendingIntent);
...
alarmManager.setRepeating(..., pendingIntent);

问题:调用 setRepeating() 是幂等的,即我是否需要显式取消 () 任何先前的警报,或者我是否可以安全地调用 setRepeating() 并完成它?

最佳答案

API documentation状态:

If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

我知道这还不是很清楚,但他们说的是 AlarmManager 将检查您传递给 setRepeating()PendingIntent 并且如果已经安排了一个具有相同 PendingIntent 的闹钟,那么该闹钟将在安排新闹钟之前取消。因此,只要您使用的 PendingIntent 没有改变,您就不需要取消任何先前的警报 (oldPendingIntent.equals(newPendingIntent) == true)

来自 PendingIntent API documentation :

equals(Object otherObj)
Comparison operator on two PendingIntent objects, such that true is returned then they both represent the same operation from the same package.

关于android - AlarmManager.setRepeating 是幂等的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2612940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com