gpt4 book ai didi

android - 如何删除已定义的待定 Intent

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:23 25 4
gpt4 key购买 nike

我对警报管理器未决 Intent 有疑问。我在文件中读到为了 Intent 解析(过滤)的目的,确定两个 Intent 是否相同。也就是说,如果它们的 Action 、数据、类型、类和类别相同。这不会比较 Intent 中包含的任何额外数据。参数

并且警报管理器的设置方法将删除或替换如果已经有针对此 Intent 计划的警报(两个 Intent 的相等性由 filterEquals(Intent) 定义)。

但是当我使用相同的概念实现时,我的警报管理器不会过滤相同的 Intent 并阻止将相同的数据添加到未决 Intent 。

我的代码是

public void setalarm() {
int[] IDs = { 1, 2, 3 };
int[] type = { 0, 1, 0 };
String[] name = { "not", "yes", "not" };
Date[] dt = { new Date(2012, 2, 28, 11, 51),
new Date(2012, 2, 29, 10, 55), new Date(2012, 2, 28, 11, 51) };

for (int i = 0; i < 3; i++) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent send = new Intent(context, service.class);
send.setAction("com.app.main");
send.setData(Uri.parse(type[i] + " " + name[i] + " " + dt[i]));
PendingIntent pintent = PendingIntent.getService(context, IDs[i],
send, PendingIntent.FLAG_CANCEL_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
try {
am.cancel(pintent);
} catch (Exception e) {
}
am.set(AlarmManager.RTC_WAKEUP, dt[i].getTime(), pintent);
}
}

当 alarm 在 start 方法上调用服务时,它会从 id 1 和 3 调用两次。但我在 id=1 和 id=3 中设置了相同的数据;

但用户只能为 id =1 设置警报,不能为 3 设置警报,因为 1st 和 3rd 相同

最佳答案

重写CancelAlarm()函数中的代码

 PendingIntent pintent = PendingIntent.getService(context, IDs[i], 
send, PendingIntent.FLAG_CANCEL_CURRENT
| PendingIntent.FLAG_ONE_SHOT);

使用 Same Intent send 和 Same Id IDs[i] 然后像这样取消 pintent

pintent.cancel();

关于android - 如何删除已定义的待定 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493776/

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