0

I am creating an alarm with some specific days selected. i want the alarm to be repeated on those week days at a point of time. But the alarm is fired as soon as i create an alarm, when past and future week days are selected.

For example, consider today as 'wednesday', if i select Tuesday and Thursday, the alarm is fired on creating it. but it should fire on the coming Tuesday and tomorrow respectively. I am unable to find the mistake in the following code.

I am unable to find what mistake i am doing.

//scheduling the alarm for repeating on selected days
public void scheduleDay(int request_code_value, String alarm_title) 
{

   Calendar calendar = Calendar.getInstance();


   for(int i = 1; days.size() >= i; i++)
   {
       if ((days.get("" + i)) == 1)
       {
           // for creating different alarms adding i to request_code_value  for uniqueness
           request_code_value = request_code_value +i;

           String filter_action = "com.ibkr.yowakeup" + request_code_value +"_time";


            IntentFilter filter = new IntentFilter(filter_action);

            registerReceiver(new AlarmReciever(), filter);


            Intent intent = new Intent(filter_action);
            intent.putExtra(getString(R.string.get_current_intent_value), request_code_value);
            intent.putExtra(getString(R.string.alarmtext), alarm_title);
            intent.putExtra(getString(R.string.alarm_time), newAlarm_Choose_Alarm_Value.getText().toString());


            Log.d(TAG,"Scheduled on " + i + " = " + days.get("" + i));

            calendar.set(Calendar.DAY_OF_WEEK, i);
            calendar.set(Calendar.HOUR_OF_DAY, selected_hour);// cal.get(Calendar.HOUR_OF_DAY);
            calendar.set(Calendar.MINUTE, selected_min);// cal.get(Calendar.MINUTE);
            calendar.set(Calendar.SECOND, 0);// cal.get(Calendar.SECOND);
            calendar.set(Calendar.MILLISECOND, 0);


            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code_value , intent, 0);

            alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 
                    AlarmManager.INTERVAL_DAY * 7,
                    pendingIntent);

        }
   }

}

Please help me. Thanks in advance.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Ravitheja
  • 761
  • 1
  • 8
  • 24

1 Answers1

0

You are trying to set the calendar by manipulating DAY_OF_WEEK. A Calendar instance always represents a specific point in time. When you call Calendar.getInstance() you are given a Calendar instance initialized to the current date and time. Let's use an example:

I'm running this code today, Sunday 8-March-2015 at 08:00. The Calendar I get from calling Calendar.getInstance() is initialized to this date/time. Since today is Sunday, the value of DAY_OF_WEEK that you would get if you called calendar.get(Calender.DAY_OF_WEEK) is Calendar.SUNDAY which is 1.

Let's say I now want to set an alarm for Tuesday. In this case your code would call:

calendar.set(Calendar.DAY_OF_WEEK, i);

where i is set to Calendar.TUESDAY which has a value of 3.

What should happen now? The Calendar instance doesn't actually have separate fields for all these parts of the date/time. It only holds a single value that represents the specific date/time. Otherwise, you could generate an inconsistency if you tried to set the DAY_OF_WEEK to SUNDAY and also set the DAY_OF_MONTH to 9 (which in March is a Monday). Also, there is no way to tell the Calendar whether it should adjust the date to the previous SUNDAY or the next SUNDAY. There is probably a way to determine this, but I wouldn't do it.

For this reason, you should never try to set Calendar fields like DAY_OF_WEEK. Instead, you should determine the current day of the week by calling calendar.get(Calendar.DAY_OF_WEEK) and then adjust the calendar instance forward by adding the correct number of days to get to the day of the week you want. Like this:

int desiredDay = // the day of the week you want to set the alarm for
int today = calendar.get(Calendar.DAY_OF_WEEK);
int numberOfDaysToAdd = desiredDay - today;
if (desiredDay < today) {
    // Desired day is earlier in the week than today, add 7 days to
    //  ensure it is in the future
    numberOfDaysToAdd += 7;
}
calendar.add(Calendar.DAY, numberOfDaysToAdd);

You might still need to check if the desired time of your alarm has already passed and set it for a date in the future using a similar algorithm. Hopefully you get the idea.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • See http://stackoverflow.com/questions/1319473/java-calendar-setcalendar-day-of-week-calendar-sunday-will-it-roll-backwards – David Wasser Mar 09 '15 at 15:20