如何删除已定义的待定意图

时间:2012-02-29 04:58:46

标签: android

我遇到警报管理器未决意图的问题。 我在文件中读过这篇文章 为了意图解析(过滤)的目的,确定两个意图是否相同。也就是说,如果他们的行为,数据,类型,类别和类别是相同的。这不会比较意图中包含的任何额外数据。 参数

并且警报管理器的set方法将删除或替换如果已安排此Intent的警报(两个意图的相等性由filterEquals(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);
    }
}

当启动方法的报警呼叫服务时,它将从id 1和3调用两次。 但是我在id = 1和id = 3中有相同的setdata;

但是用户只能为id = 1而不是3设置闹钟,因为第1和第3是相同的

2 个答案:

答案 0 :(得分:14)

重写CancelAlarm()函数

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

使用Same Intent send and Same Id IDs[i],然后取消pintent这样的

pintent.cancel();

答案 1 :(得分:4)

只需重新创建相同的PendingIntent(使用相同的附加内容)并使用取消方法

  

如果创建应用程序稍后重新检索相同类型的   PendingIntent(相同的操作,相同的Intent操作,数据,类别,   和组件,以及相同的标志),它将收到一个PendingIntent   如果仍然有效,则表示相同的令牌,因此可以调用   取消()删除它。

示例:

am.cancel(pintent)