Keep track of multiple alarms with Android

I need to set some alarms that are repeated weekly in order to automatically put the phone into vibration when the user is in some class (this is an academic application), and then reset when the class ends.

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

The user can cancel classes and register in classes (but not directly in the application), as well as update and reflect in the database, which stores only the current classes. Therefore, if I have an alarm setting for a class that no longer exists, how can I cancel it if I cannot replicate the PendingIntent?

One solution that I see here is to create a db table to track the current alarms, then I will have full control over them, another way is to cancel and reset all alarms when the class list is updated, but it takes quite a lot of time , and the third, but less friendly option is to just wait until the user loads the phone, and when restarting the alarms, just set the ones that I need (correctly, if I am mistaken in this loading behavior). Is there a fourth option that I should try, or one of them if it is good enough?

+3
source share
1 answer

. NEXT, , , . , , , , .

, . Set , .

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);
    }
}
+4

All Articles