0

I want to set an alarm on a particular day of week like Sunday. I know how to do that.

Code:

   if (!repeatDays.equals("")) {
                                    String[] repeatDays = this.repeatDays.split(",");
                                    String[] weekDays = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
                                    if (intent == null) {
                                        for (int i = 0; i < weekDays.length; i++) {
                                            for (int k = 0; k < 1; k++) {
                                                if (weekDays[i].equals(repeatDays[k])) {
                                                    mCalendar.set(Calendar.DAY_OF_WEEK, i + 1);
                                                    mCalendar.set(Calendar.MONTH, mMonth);
                                                    mCalendar.set(Calendar.YEAR, mYear);
                                                    mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
                                                    mCalendar.set(Calendar.MINUTE, mMinute);
                                                    mCalendar.set(Calendar.SECOND, 0);
                                                    new AlarmReceiver().createExactAlarm(getApplicationContext(), mCalendar, reminderId);
                                                }
                                            }
                                        }
                                    }
}

Problem: Suppose If today is Monday, and if I'll set an alarm for all rest of the day, it just works fine, but suppose today is Saturday,and if I set an alarm for any other previous days, like Monday to Friday, Alarm manager understands that it is a past day, so it triggers the alarm immediately. But what I want is if I even select past days, it should calculate as that day of the next upcoming week. This is my requirement.

Note: I set day of week based on current system date and from that I got mDate, year & month, and that I pass in calender object:

mDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
  • Please make sure you are using reminder id different from the previous one. – Divya SIngh Apr 16 '19 at 11:38
  • if it is a day of week before today, add 7 days to calendar So alarm will be triggered same day but one week later mCalendar.add(Calendar.DATE, 7); – arslanbenzer Apr 16 '19 at 11:40
  • @DivyaSIngh Sorry Divya, I've to use same id, and what I'm doing is I called setExact, so once first alarm trigeers suppose for Tuesday and comes in onReceive, I set alarm for the next scheduled day, may be Friday. –  Apr 16 '19 at 11:40
  • @arslanbenzer Please explain through code –  Apr 16 '19 at 11:40
  • @arslanbenzer Can you write some code? –  Apr 16 '19 at 11:53
  • if ( mCalendar.getTime( ).before ( new Date ( ) ) ) mCalendar.add(Calendar.DATE, 7); if the day is past, add 7 days just before you call new AlarmReceiver().createExactAlarm () – arslanbenzer Apr 16 '19 at 11:59
  • @arslanbenzer, but what problem will occur is, it will increase directly 7 days, so even suppose my one day is past and one day is future (Because I can choose multiple days) so future selected day will also go for next week? and alarm will not trigger for any of that day in current week? –  Apr 16 '19 at 12:04
  • we have an if condition, if the day is not past we do not add 7 days – arslanbenzer Apr 16 '19 at 12:07

1 Answers1

0

@Jimmy why you need to use same reminder id? while we can set repeat alarm by using different id's.Also Suggest you to go with pending indent. Please go through below given links which has same requirement as yours. Hope these will work!!

How can i get the repeat alarm for week days using alarm manager?

how to repeat alarm week day on in android

Divya SIngh
  • 129
  • 1
  • 11