0

I have an AlarmManager that notifies me every 10 seconds. Everything works just fine but for some reason I can't cancel the alarm. Here's my code.

public class AlarmNotifReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
//things
}

public void SetAlarm(Context context)
{
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmNotifReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pi); // Millisec * Second
}

public void CancelAlarm(Context context)
{
    Intent i = new Intent(context, AlarmNotifReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);
}


}

as expected I call this CancelAlarm() method from my other classes but for some reason it does not cancel and keep notifying me like nothing happened. Note: SetAlarm() also works just fine.

Thanks in advance.

Suhrahj Rothgar
  • 283
  • 6
  • 21

5 Answers5

1

Try to cancel Pendingintent--

PendingIntent.getBroadcast(context, 0, intent, 
                           PendingIntent.FLAG_UPDATE_CURRENT).cancel();

Hope this helps

Pramod
  • 1,123
  • 2
  • 12
  • 33
1

Alarm should be created and cancelled on Same Pending intent.In your case you are creating Pending Intent Twice.

Your code should look like below.

public class AlarmNotifReceiver extends BroadcastReceiver {

    PendingIntent pi;
    AlarmManager am;
    Intent i;

    @Override
    public void onReceive(Context context, Intent intent) {
        pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        i = new Intent(context, AlarmNotifReceiver.class);
        //things
    }

    public void SetAlarm(Context context) {

        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 1000, pi);
    }

    public void CancelAlarm(Context context) {
        am.cancel(pi);
    }


}   
Vipul
  • 27,808
  • 7
  • 60
  • 75
1

Try this.

Android: Get all PendingIntents set with AlarmManager

To cancel all alarm, first you have to find all the pending intent for that and cancel alarm using that.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Solution
  • 604
  • 4
  • 10
0

Try like this

public class AlarmNotifReceiver extends BroadcastReceiver {

AlarmManager am = null;

 @Override
 public void onReceive(Context context, Intent intent)
{
 Intent i = new Intent(context, AlarmNotifReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
//things
}

public void SetAlarm(Context context)
{
am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pi); // Millisec * Second
}

public void CancelAlarm(Context context)
 {

am.cancel(pi);
}


}
Sonali8890
  • 2,005
  • 12
  • 16
0

Your problem is that you're creating a NEW intent instead of cancelling the existing one. Function SetAlarm should place the intent into a "global" variable and the CancelAlarm function should just call am.cancel(global_pi);

The above worked for me in a service, but:

If this is impossible (since that's a Receiver), try setting a new alarm with the (new) intent first.

velis
  • 8,747
  • 4
  • 44
  • 64
  • As long as you're putting `FLAG_CANCEL_CURRENT` flag, then it knows to match and cancel / update. – gunar Feb 25 '14 at 13:29