0

I am new to android and have problems with setting a repeating alarm. The app is like this: user selects time using a timepicker, and selects several days to set the alarm. I use this code to implement this task:

for(int i=0; i < alarm.getDaysOfTheWeek().size(); i++){

        // here is a switch block to assign daysUntilAlarm1 
        //to a particular day from the list of selected days, like 
        //daysUntilAlarm1 = Calendar.SUNDAY;


        calendar.set(Calendar.DAY_OF_WEEK, daysUntilAlarm1);

        calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour()); // hour picked by user
        calendar.set(Calendar.MINUTE, alarm.getMinute());


        pendingIntent = PendingIntent.getBroadcast(AlarmManagerActivity.this, i, intentToAlarmReceiver, 0);
        pendingIntents.add(pendingIntent);


        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);

The problem is when I test alarms, I change the system time to the next alarm day. Everything works fine. But when I change the system time to the past, like last week, the alarm doesn't work. So if user will have to change the time of the device to past, the app will not work. Can anyone help me, please?

Zimeni
  • 2,529
  • 2
  • 8
  • 6
  • You need to register a Receiver for [`ACTION_TIME_CHANGED`](https://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_CHANGED), and reset your alarms as needed. – Mike M. Sep 03 '17 at 07:28
  • @MikeM. thank you for your reply. But could you give an example, please? I searched for it, and found that it has to be Intent intent = new Intent(Intent.ACTION_TIME_CHANGED); And that intent should be sent to the PendingIntent later. The problem is that my intent is set to Alarm_Receiver class and if I change it, the alarm doesn't work – Zimeni Sep 03 '17 at 08:44
  • No, you don't send the `ACTION_TIME_CHANGED` broadcast yourself. The system sends it whenever the time changes. You just need to listen for it, by registering a Receiver in your manifest for that action. Here's an example that also listens for time zone changes, which you might want to handle as well: https://stackoverflow.com/a/44424579. – Mike M. Sep 03 '17 at 09:08
  • @MikeM. ok, i understand) thank you! – Zimeni Sep 04 '17 at 09:10

0 Answers0