如何创建不同的 pendingintent 以便 filterEquals() 返回 false? [英] How to create different pendingintent so filterEquals() return false?

查看:22
本文介绍了如何创建不同的 pendingintent 以便 filterEquals() 返回 false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AlarmManager 来设置重复意图,但它造成了一些小麻烦,所以希望任何人都可以提供帮助.

I'm using AlarmManager to set up repeating intents but it has caused some little troubles so hope anyone could help.

总结

有 2 个待处理的意图.一个每天运行 1000 个,另一个运行在 2000 个.每个都包含一个来自数据库的行 ID,用于识别目的.代码如下所示:

There are 2 pending intents. One runs at 1000 and another runs at 2000 every day. Each contains a row id from the database for identification purpose. The code looks something like this:

Intent i = new Intent(mContext, ScheduleReceiver.class);
i.putExtra(RuleDBAdapter.KEY_ROWID, (int)taskId);
PendingIntent pi =PendingIntent.getBroadcast(...);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi); 

删除:

问题是当我们需要删除其中之一时.删除未决意图的正确方法是设置一个相同的意图,然后从 AlarmManager 调用取消.

The problem is when we need to delete one of them. The proper way to delete a pending intent is to set up an identical one and then call cancel from AlarmManager.

Android 文档:

Android Documentation:

公共无效取消(PendingIntent 操作)
删除任何具有匹配意图的警报.任何类型的任何警报,其 Intent 与此匹配(由 filterEquals(Intent) 定义)都将被取消.

public void cancel (PendingIntent operation)
Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

public boolean filterEquals(意图其他)
出于意图解析(过滤)的目的,确定两个意图是否相同.也就是说,如果它们的动作、数据、类型、类和类别是相同的.这不比较意图中包含的任何额外数据.

所以在上面的例子中,如果我做了一个相同的意图然后取消,上述两个意图都会被取消,因为它们来自同一个类/相同的动作等(除了额外的"" 数据是 rowId 但 filterEquals 不关心额外的数据.

So in the above example, if I make an identical intent then cancel, both of the above intents will get canceled, because they are from the same class/same action etc (except the "extra" data is rowId but filterEquals doesn't care about extra data).

有什么解决方法吗?

推荐答案

您可以在创建待处理意图时尝试使用 requestCode 参数:

You could try using the requestCode parameter when creating your pending intent:

PendingIntent pi =PendingIntent.getBroadcast(mContext, yourUniqueDatabaseId,i,PendingIntent.FLAG_ONE_SHOT);

这应该创建一个用于匹配目的的唯一意图..

This should create an Intent that is unique for matching purposes..

这篇关于如何创建不同的 pendingintent 以便 filterEquals() 返回 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆