10

I use an Alarm to fetch data from server. I like to give user the option to start and stop the alarm. This means I have to check and see if alarm is already set. I found some code that tells me if the alarm is already set:

Intent I = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_NO_CREATE);
found = (P!=null);

if the Alarm is already set I cancel it but if it is not set then I set it (like a toggle)

Problem is this works only once. The first time the above code to check existing alarms will return null indicating no alarm but after I cancel the alarm once it returns a pointer to something but alarm is not running.

here is the code to set alarm

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
Intent I = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, P); 

and here is the code to cancel an alarm:

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
Intent I = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(P);

Am I to reset something after canceling an alarm to make it's PendingIntent go away.

Kemal
  • 101
  • 1
  • 1
  • 3
  • 1
    I'm not really sure why you are performing the check to see if an alarm already exists. Can you explain your reasoning please? – dave.c Feb 10 '11 at 23:06
  • I wanted to have a button that acts as a toggle on/off. Also use the button as an indicator if my alarm setting was lost due to app-crash. I use the alarm to update a widget when user clicks on the widget it opens an activity where user can see if the alarm is set or not. What I am afraid of is after I set the alarm it will be turned off and user will not know that is is off. – Kemal Feb 11 '11 at 17:22

1 Answers1

52

When canceling the AlarmManager do not use a PendingIntent with a flag of FLAG_CANCEL_CURRENT. Instead, cancel the PendingIntent explicitly after canceling the alarm:

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent p = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
am.cancel(p);
p.cancel();
JDJ
  • 4,198
  • 3
  • 22
  • 43
Dimitar Darazhanski
  • 1,948
  • 17
  • 22
  • 1
    This should receive +50 because none of the other posts address canceling the `PendingIntent` as well as the `AlarmManager`, and I am using `(PendingIntent.getBroadcast(getBaseContext(), 0, checkIntent, PendingIntent.FLAG_NO_CREATE) != null);` to check if my alarm is running and this is the only thing that worked without restarting my activity. Great job. – midiwriter Jun 14 '13 at 03:29