4

I need to set a number of alarms that repeat weekly, to automatically put the phone on vibrate when the user is on some class (it's an academic app) and then reset the volume when the class finishes.

I already have the alarms being created, but I must have a way to deactivate all of them if the user disables this feature. I also know I must do so by passing and intent with the same parameters to the AlarmManager.cancel() method, but the problem here is:

The user can cancel classes and enroll in classes (but not directly in the app), and that's updated and reflected on the DB, that keeps just the current classes. So if I have an alarm setup to a class that doesn't exists anymore, how can I cancel it if I can't replicate the PendingIntent?

One solution I see here is to create a db table to track the current alarms, then I'd have full control on them, another way would be to cancel and reset all alarms when the class list is updated, but doing so takes a fair amount of time, and a 3rd but less friendly option would be to simple wait for the user to boot the phone, and when reseting the alarms just set the ones I need (correct if I'm wrong on this boot behaviour). Is there a fourth option I should try, or one of these if good enough?

Rodrigo Castro
  • 1,573
  • 14
  • 38

1 Answers1

4

First you do not need to set up multiple alarms. Simply set up the NEXT alarm and then when that alarm fires, set up the new next alarm. This also makes it easy to set up the alarms again when they are lost if the phone is rebooted as you only have one alarm to think about.

I use this small routine to set my alarm. The value of the Set parameter determines if the alarm is set or cancelled.

public static void SetMyAlarm(Context c, long AlarmTime, Boolean Set) {
    Intent intent = new Intent(c, AlarmReceiver.class); // The broadcast receiver that will handle my alarm 

    int UniqueID =8192; // id for this specific alarm, use a different id for each separate alarm

    PendingIntent sender = PendingIntent.getBroadcast(c, UniqueID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    if (Set) {
        am.set(AlarmManager.RTC_WAKEUP, AlarmTime, sender);
    } else {
        am.cancel(sender);
    }
}
Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • Thanks, that's actually a pretty smart way to do it. This way I can easily track the active alarm using SharedPreferences. I'll try this here – Rodrigo Castro May 02 '12 at 17:46
  • Not to be pedantic, but the boolean in the method signature could (should?) be a primitive. There's no reason for it not to be. – Michael De Soto Oct 31 '16 at 14:48
  • That actually is very pedantic but yes it should. I'm sure you will find many, many typos on this site. Four years ago, I don't think the OP cared that I accidentally typed a capital B whilst I was trying to help him. – Kuffs Nov 01 '16 at 09:04
  • @Kuffs Nice answer. I have user entered due dates stored in a SQLite database. I want the user to receive a notification 24 hours before each due date is reached. So would you suggesting setting up one alarm for the nearest due date that will soon be reached and therefore I just have to search the due date column for the nearest due date? – AJW Mar 21 '18 at 03:25
  • That's pretty much what I said in my answer so yes – Kuffs Mar 21 '18 at 14:24