Ángel Fas
Ángel Fas

Reputation: 373

AlarmManager can't cancel an alarm

Let's see if someone found this problem and can help me to solve the mistery. I have the following situation. The app is a simple to-do list:

public class NewToDo extends Activity {

    private PendingIntent pendingIntent; 
    private AlarmManager manager;
  ...

When the user creates a to-do with a notification, I create it in one of the methods of this class like this:

  int interval = 30000;
  Intent alarmIntent = new Intent(this, AlarmReceiver.class);
  /*Let's let the putExtras apart, as documentation says they're not considered when comparing intents, but I add some of them*/
  manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, alarmIntent, alarmIntent.FILL_IN_DATA);
  /*Printing _id we see 3, for instance, but is a unique identifier to associate the notification with this to-do item*/
  /*I use the flag alarmIntent.FILL_IN_DATA because I want to send extras to create a custom notification*/                  
  manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + interval, interval,
                pendingIntent);

I quit from the app (but no difference if I don't quit).Then the notification arrives correctly each 30 seconds, and selecting it the app launchs on the detailed view of the item. An actionbar button allows editting it, calling again the same class, NewToDo. While editting I can cancel the notifications associated with the to-do, and my code makes that like this -in the NewToDo class-:

  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent i = new Intent(this, AlarmReceiver.class); //creating a matching Intent

  Log.v(getString(R.string.app_name), "_id: " + _id);  //the 3 aforementioned

  PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), _id, i, alarmIntent.FILL_IN_DATA);
  alarmManager.cancel(displayIntent);

But the notifications keeps appearing each 30 seconds, so they're not cancelled.

I saw the following suggestion in another post:

"Intent intentYouWantToCancel = new Intent(this, AlarmReceive.class); intentYouWantToCancel.setAction("aRelevantString");

when you set the alarmmanager as well as when you want to delete it. "

So I put in both intents as 'aRelevantString' a cast to String of the unique identifier. But same problem.

Also changed the getBaseContext() for this, just in case, but no difference.

No errors, just the notifications keep repeating ignoring my cancel. Any idea?

PS: I restart the AVD each time I run it. I realised that the way I was using the print of the filterEquals had no sense at all (too many hours with this), but that doesn't affect the rest of the question.

Upvotes: 1

Views: 2017

Answers (1)

Ángel Fas
Ángel Fas

Reputation: 373

I finally solved it like this. To create the alarm:

public class NewToDo extends Activity {

    private PendingIntent pendingIntent; 
    private AlarmManager manager;
    ...

int interval = 30000;
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("id", _id);
pendingIntent = PendingIntent.getBroadcast(this, _id, alarmIntent,0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + interval, interval,
                pendingIntent);

To cancel that concrete alarm:

manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), _id, i, 0);

manager.cancel(pendingIntent);
pendingIntent.cancel();

Upvotes: 1

Related Questions