简体   繁体   中英

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.')

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.

Then I call 3 times the cancel alarm and the ids generated match for each alarm upon create and cancel.

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)

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.

Changed the way I generate the _id to exclude the dates (which I generated with new Date()) and not it works

By bad, I didn't see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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