21

I am building an app that set 2 alarms for each day of the week (at a certain hour and minute), the alarms repeat week after week forever.

Now the point is: if the user changes the alarms, I will need cancel the previously set alarms.

Is there a way to simply cancel all the alarms set by my application ?

Vigbyor
  • 2,568
  • 4
  • 21
  • 35
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

3 Answers3

18

if you are canceling previous alarms then in PendingIntent your flag should be PendingIntent.FLAG_CANCEL_CURRENT. It will prevent generating a new PendingIntent if it is already created. And make sure that before setting in alarm just cancel that same PendingIntent and after that set your alarm. You should try like this:

AlarmManager 2AlarmsInWeekAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService/getActivity(context, int, intent, PendingIntent.FLAG_CANCEL_CURRENT);

2AlarmsInWeekAlarmManager.cancel(pendingIntent);

and then you may use set or setRepeating method. In your case it should be

2AlarmsInWeekAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, "timeInMillis", "repetitionTimeInMillis", pendingintent);

This guarantees that before setting an alarm will cancel all previously alarm with the same PendingIntent.

Hope you got this!

Michael
  • 113
  • 3
  • 13
imthegiga
  • 1,126
  • 8
  • 13
  • I need a clarification. cancel() documentation says "Remove any alarms with a matching Intent." Does this mean that the request code im getBroadcast (Context context, int requestCode, , Intent intent, int flags) needs to be same also? Please clarify. – user2731584 Jan 07 '15 at 18:56
  • 1
    hello, document doesn't say much about `requestCode`. But if you see `getBroadcast` in [PendingIntent](http://developer.android.com/reference/android/app/PendingIntent.html) which says, it retrieves a `PendingIntent`. So **as per my knowledge** if you put different `requestCode`s then you'll get 2 different `PendingIntent`s, hence while canceling it'll be the other `PendingIntent` instead of desired one! Also look into [this link](http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT). – imthegiga Jan 08 '15 at 10:49
  • Thanks for getting back to me. Let me read the documentation again. I also didn't see anyway to request the list of alarms scheduled by a class. Any idea? – user2731584 Jan 08 '15 at 12:11
  • I'm sorry I didn't get it :/ – imthegiga Jan 08 '15 at 13:25
  • I wants to know if there is any mechanism to check if there is any alarms set by the application - without using alarm ID. Currently I am checking to see if the alarmID is set or not. But that wouldn't tell me if anything else is set by the activity. – user2731584 Jan 14 '15 at 05:54
  • 3
    to cancel the alarm you need to re-create the exact `PendingIntent`, I guess there is no way to retrieve it directly from code. Still you can see pending alarms in shell! If you're familiar with adb shell you can achieve it with commands: `adb shell dumpsys alarm > log.txt` where `log.txt` will have all pending alarms with package names, so you can check yours. I'm not sure how to get it from code. – imthegiga Jan 15 '15 at 09:05
12

I think you could get an eye on : AlarmManager.Cancel

And on that Question/Answer: Android: Get all PendingIntents set with AlarmManager

As stated in there you can't ask to the AlarmManager to tell you what PendingIntent are in it. But I think you can make some PendingIntent similar to the one you want to cancel ;).

Community
  • 1
  • 1
BigbangRevo
  • 330
  • 5
  • 12
5

I had same problem for cancelling alarms, and solved it. What you should do is simply do -

  1. When you create alarms, save request code of PendingIntent object (to shared preference).
  2. At later, when you want to cancel the alarm, create the same PendingIntent with the same request code (obviously from shared preference).
  3. Call cancel() of AlarmManager and pass the PendingIntent object in it, and alarm will be cancelled.

    private void cancelAlarm(int requestCode) {
    
    AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(),requestCode,new Intent(this, MyBroadCastReceiver.class),0);
    alarmManager2.cancel(pendingIntent2);
    
    Toast.makeText(getApplicationContext(), "Alarm Cancelled - "+ requestCode, Toast.LENGTH_LONG).show();
    

    }

Sohail Ansari
  • 131
  • 1
  • 6