47

I have met same issue like this . Delete alarm from AlarmManager using cancel() - Android

" I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.

However when I call AlarmManager's cancel() method, the alarm isn't deleted."

In order to set :

            Intent myIntent = new Intent(getApplicationContext(),
                    SessionReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    getApplicationContext(), 1, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(),
                    pendingIntent);

In order to delete :

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent myIntent = new Intent(getApplicationContext(),
            SessionReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.cancel(pendingIntent);

But this doesn't remove an alarm registered. Thanks in advance.

Community
  • 1
  • 1
Vladimir Homola
  • 473
  • 1
  • 4
  • 4

2 Answers2

110

The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).

For a quick fix, this should work:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                 getApplicationContext(), 1, myIntent, 0);

alarmManager.cancel(pendingIntent);

In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:

Setting:

Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                getApplicationContext(), 1, myIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);

Cancelling:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                getApplicationContext(), 1, myIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Oh. I see the issue . I should have used same requestCode ! Thanks so much for your help, – Vladimir Homola Mar 09 '15 at 03:39
  • Can you please help me to solve https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity#51542099 @DanielNugent – Zhu Jul 26 '18 at 15:25
  • 47
    why didn't Google implement is as `AlarmManager.cancel(int requestCode)` ? The OS then handles the rest from underlying details such as app namespace. Certainly would be a more userfriendly API – Someone Somewhere Nov 06 '18 at 13:14
  • 1
    So basically the requestCode is an identifier for the alarm? – Mitulát báti Apr 02 '19 at 20:43
  • 2
    No: requestCode isn't the identifer for the alarm. The PendingIntent is the identifier, and requestCode is part of the PendingIntent's key props (along with the underlying Intent itself, which has its own identifying keys). – Gabriel Jun 07 '19 at 13:18
  • Does setting also updates (to a new trigger time, for example)? Or I have to cancel and set? – android developer Sep 05 '21 at 06:54
6

Initially for me also not worked,After Seeing many posts i realized that the pending intent to be canceled should be same as the original pending intent that was used to schedule alarm. The pending intent to be cancelled should have set to same action and same data fields,if any have those were used to set the alarm.After setting the same ACTION and data values though i'm not using them,only cancelled the Alarm.

RAMAKRISHNA
  • 69
  • 1
  • 2
  • This worked for me. I was using FLAG_ONE_SHOT for pending while setting alarm and FLAG_UPDATE_CURRENT when cancelling it. Replacing FLAG_UPDATE_CURRENT with FLAG_ONE_SHOT(which was used while set alarm) for cancel gave the desired result – Trupti Nasit May 04 '22 at 07:13