1

I would like to schedule a repeating alarm daily in android, I was able to get this done partially in which alarm only triggers once and never get to repeat daily, Here is the my code snippet

    /* Schedule the alarm based on user preferences */
public void scheduleAlarm(Context context) {
    String alarmPref;

    manager = AlarmManagerProvider.getAlarmManager(context);

    String keyReminder = context.getString(R.string.pref_key_reminder);
    String keyAlarm = context.getString(R.string.pref_key_alarm);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    boolean enabled = preferences.getBoolean(keyReminder, false);

    //Intent to trigger
    Intent intent = new Intent(context, ReminderService.class);
    operation = PendingIntent
            .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    if (enabled) {
        Calendar calendar = Calendar.getInstance();

        alarmPref = preferences.getString(keyAlarm, "12:00");

        calendar.setTimeInMillis(System.currentTimeMillis());

        String[] str = alarmPref.split(":");
        int hourDay = Integer.parseInt(str[0]);
        int minute = Integer.parseInt(str[1]);

        calendar.set(Calendar.HOUR_OF_DAY, hourDay);
        calendar.set(Calendar.MINUTE, minute);

        if (Calendar.getInstance().after(calendar)) {
            calendar.add(Calendar.DATE, 1);
        }

        Log.d(TAG, "Scheduling quiz reminder alarm" + hourDay);
        Log.d(TAG, "Scheduling quiz reminder alarm" + minute);

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, operation);
    } else {
        Log.d(TAG, "Disabling quiz reminder alarm");
        manager.cancel(operation);
    }
}

I would like to make this alarm receiver repeats daily on selected day. At present it works only once and never repeats at the exact day daily

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Delaroy
  • 55
  • 2
  • 9

0 Answers0