1

My application creates an alarm that repeats every day. Since it is not possible to know if an alarm is already registered, I create the alarm every time the application is launched.

Here is the function that is called at the beginning of the main activity (inside onCreate()):

public static void setAlarm(Context context) {
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Calendar firingCal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));
    Calendar currentCal = (Calendar)firingCal.clone();

    Random rnd = new Random();

    // Each day between 1:00 AM and 1:30 AM (some jitter added)
    firingCal.set(Calendar.HOUR_OF_DAY, 1);
    firingCal.set(Calendar.MINUTE, rnd.nextInt(30));
    firingCal.set(Calendar.SECOND, 0);

    long intendedTime = firingCal.getTimeInMillis();
    long currentTime = currentCal.getTimeInMillis();

    if (intendedTime >= currentTime) {
        //set from today
        alarmManager.setInexactRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
    } else {
        //set from next day
        firingCal.add(Calendar.DAY_OF_MONTH, 1);
        intendedTime = firingCal.getTimeInMillis();

        alarmManager.setInexactRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

Is it OK to recreate the alarm each time the application is launched? Should I use some flags on the PendingIntent (like update or cancel existing PendingIntent)?

Thanks!

Update: is it a problem if I use flag 0 instead of PendingIntent.FLAG_UPDATE_CURRENT from a performance perspective?

  • *Since it is not possible to know if an alarm is already registered* <= not true, i have seen the answer here on SO ... – Selvin Jan 23 '15 at 16:41
  • If you recreate the alarm then you should use the proper `PendingIntent` flag. But why don't you just save in your `SharedPreferences` if the alarm is already registered? – Xaver Kapeller Jan 23 '15 at 16:46
  • I can see in your code that your Pending intent has always the **same id** (0). So, the next time you register it, it will overwrite the previous one, if any. – Phantômaxx Jan 23 '15 at 16:47
  • hmm what about: http://stackoverflow.com/questions/4556670/how-to-check-if-alarmmamager-already-has-an-alarm-set – Selvin Jan 23 '15 at 16:48
  • @Xavier Kapeller What is the difference of using flag 0 and PendingIntent.FLAG_UPDATE_CURRENT? Some performance issues? Yes, it is a good idea, but the alarm gone away when device reboot! – Kevin Vuilleumier Jan 23 '15 at 16:48
  • `the alarm gone away when device reboot!` If you want your alarm to survive reboots, you need a different approach. See this: https://developer.android.com/training/scheduling/alarms.html – Phantômaxx Jan 23 '15 at 16:50
  • Yes, but it is not a problem if the alarm gone away. My question is more from a performance and good practices perspective ;) – Kevin Vuilleumier Jan 23 '15 at 16:51

0 Answers0