简体   繁体   English

无法取消Android Alarm Manager的警报

[英]Can't cancel alarms for Android Alarm Manager

I'm trying to set several alarms with my app, and cancel them upon request. 我正在尝试使用我的应用程序设置多个警报,并根据请求取消它们。

I've read a lot of topics about the same, and went through the documentation, but it seems like it doesn't work. 我已经阅读了很多关于相同的主题,并浏览了文档,但似乎它不起作用。

Documentation says that cancel will try to find an intent that matches the one I provide with ' filterEquals '('Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.') 文档说,取消将尝试找到一个与我提供的' filterEquals '匹配的意图('确定两个意图是否相同,以达到意图解析(过滤)的目的。也就是说,如果他们的行为,数据,类型, class和类别是相同的。这不会比较意图中包含的任何额外数据。')

I'm providing same action, data, type, class, and category, so why does it not work? 我提供相同的操作,数据,类型,类和类别,为什么它不起作用?

Create alarm: 创建警报:

    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    int interval = 30000;

    Intent intent = new Intent(getApplicationContext(), Broadcast.class);        
    intent.putExtra("alarmTime",alarmTime);
    intent.putExtra("reminder1",reminder1);
    intent.putExtra("reminder2",reminder2);
    intent.putExtra("title",title);
    intent.putExtra("message",message);
    intent.putExtra("vibrate",vibrate);
    intent.putExtra("sound",sound);
    intent.putExtra("name",name);

    //Create _id from data input. is unique
    int _id = 0;
    for (char i: name.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    for (char i:title.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    for (char i:message.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    _id += (int) alarmTime.getTime();
    _id += (int) reminder1.getTime();
    _id += (int) reminder2.getTime();
    Log.e("ALARMS", "Creating alarm with id: "+_id);
    intent.setData(Uri.parse("custom://" + _id));
    intent.setAction(String.valueOf(_id));
    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(getApplicationContext(), _id, intent, 0);

    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + interval, pendingUpdateIntent);

Cancel alarm 取消闹钟

AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getApplicationContext(), Broadcast.class);
    intent.putExtra("alarmTime",alarmTime);
    intent.putExtra("reminder1",reminder1);
    intent.putExtra("reminder2",reminder2);
    intent.putExtra("title",title);
    intent.putExtra("message",message);
    intent.putExtra("vibrate",vibrate);
    intent.putExtra("sound",sound);
    intent.putExtra("name",name);

    int _id = 0;
    for (char i:name.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    for (char i:title.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    for (char i:message.toCharArray()){
        _id += Character.getNumericValue(i);
    }
    _id += (int) alarmTime.getTime();
    _id += (int) reminder1.getTime();
    _id += (int) reminder2.getTime();
    intent.setData(Uri.parse("custom://" + _id));
    intent.setAction(String.valueOf(_id));
    PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(getApplicationContext(), _id, intent, 0);
    Log.e("ALARMS", "Cancelling alarm with id: "+_id);
    // Cancel alarms
    try {
        manager.cancel(pendingUpdateIntent);
    } catch (Exception e) {
        Log.e("ALARMS", "AlarmManager update was not canceled. " + e.toString());
    }

So I call 3 times the create alarm with different parameters, so my _id is different each time. 所以我用不同的参数调用3次创建警报,所以我的_id每次都不同。

Then I call 3 times the cancel alarm and the ids generated match for each alarm upon create and cancel. 然后我拨打3次取消警报,并在创建和取消时为每个警报生成匹配的ID。

So the cancel runs but my alarms still get fired. 所以取消运行但我的警报仍然被解雇。

My alarms trigger a broadcast receiver that will output a notification per alarm. 我的警报触发广播接收器,每个警报输出一个通知。

I forgot to mention that I tried with different flags for the PendingIntent but none work (FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT) 我忘了提到我尝试使用PendingIntent的不同标志但没有工作(FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT)

Seems like I was feeding different dates to the alarms, and the _id's were different by a couple of numbers (start and finish were the same), this resulted on calling a cancel on _id values that were different and not working. 好像我正在为警报提供不同的日期,并且_id是不同的几个数字(开始和结束是相同的),这导致调用取消_id值不同而且不起作用。

Changed the way I generate the _id to exclude the dates (which I generated with new Date()) and not it works 更改了我生成_id的方式以排除日期(我使用新的Date()生成的日期)而不是它的工作原理

By bad, I didn't see it. 糟透了,我没有看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM