Android AlarmManager: is there a way to cancell ALL the alarms set?

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.

  • Android ArrayAdapter.Add method not working
  • How to find out if ListView has scrolled to top Most position?
  • Unable to resolve dependencies for Android SDK Tools
  • Keyboard layout hiding android action bar?
  • LocationRequest smallest displacement and accuracy
  • Save little information as setting in android (like first time that app is run)
  • Is there a way to simply cancel all the alarms set by my application ?

    Related posts:

    test if a button starts a new activity in android junit (pref without robotium)?
    Media Control on Lock Screen like Google Play Music in android?
    How to execute APKs on a customized data directory?
    Google Drive API doesn't play well with ProGuard (NPE)
    Android: Robolectric does not support API level 1
    How to create custom text-to-speech engine
  • Streaming video from Android camera to server
  • What is exact meaning of “kitchen sink” in programming?
  • Proximity alert for locations saved at server
  • Exclusive CSS for iPhone/Android
  • Get GPS Location in a Broadcast Receiver/or Service to Broadcast Receiver data transfer
  • Automatically versioning Android project from git describe with Android Studio/Gradle
  • 2 Solutions collect form web for “Android AlarmManager: is there a way to cancell ALL the alarms set?”

    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 ;).

    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);

    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!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.