thedeepfield thedeepfield - 20 days ago 7
Android Question

AlarmManager not Canceling my setRepeating Alarm

I have an activity (setting alarm) that creates a repeating alarm, I need to cancel that alarm from another activity (cancel alarm). I am unable to cancel the alarm. What am I doing wrong?:

public class settingalarm...

am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alrm = new Intent(context, z.class);
alrm.setAction(ALARM);
alrm.putExtra(x, x);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, alrm, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, 0, 30000, alarmIntent);


I have another activity that needs to cancel the existing alarm:

public class cancelingalarm...

am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alrm = new Intent(context, z.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, alrm, 0);
am.cancel(alarmIntent);


Thanks in advance!

Answer

You need to use an equivalent Intent for the PendingIntent you create. In this that means setting the same action. BTW, since you are using an explicit Intent (you specify the class name), there is really no need to add an action as well.

In short: either remove alrm.setAction(ALARM); or use it in both places.

Comments