Vladimir Homola
Vladimir Homola

Reputation: 503

How to cancel alarm from AlarmManager

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.

Upvotes: 50

Views: 57014

Answers (2)

Daniel Nugent
Daniel Nugent

Reputation: 43322

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);

Upvotes: 114

RAMAKRISHNA
RAMAKRISHNA

Reputation: 69

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.

Upvotes: 6

Related Questions