Lazy Ninja
Lazy Ninja

Reputation: 22527

How to cancel an AlarmManager

I have setup an alarmManager as follows:

    Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);

    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

The following is supposed to cancel it, but it always fail. What am I missing?

 AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, start);
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength);

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

I have read lots of answers on StackOverflow but still could not figure out what's the problem.
Thanks in advance!

Upvotes: 1

Views: 3408

Answers (3)

Lazy Ninja
Lazy Ninja

Reputation: 22527

I will post my answer here hoping that would help others saving some time.
Somehow adding the flag PendingIntent.FLAG_UPDATE_CURRENT in both sides solve the problem.

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

solved the problem.
Setting Alarm

 Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);

    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

Cancel Alarm

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

Upvotes: 2

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

For AlarmManager to cancel an event, it needs to compare the two Intents.

This is achieved through filterEquals (Intent other) method which says

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 am not sure why it does not matches when the class is same.

I too had the same problem, to overcome with that I have set a data also.

See if this helps u.

EDITED

while setting up the Alarm

    Intent intent = new Intent(TopActivity.this, RecordActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
    intent.putExtra(Utils.KEY_REC_START_TIME, start);
    intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 
    saveTimeAndLength(start, recordLength);

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);

And when need to cancel

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, start);
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength);
intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
try {
    alarmManager.cancel(pintent);
    Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

Upvotes: 3

Naveen R
Naveen R

Reputation: 55

You need to create your pending intent and then cancel it

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

Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);

// Cancel alarms
try {
    alarmManager.cancel(pendingUpdateIntent);
} catch (Exception e) {
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}

Upvotes: 0

Related Questions