6

I am trying to develop alarm functionality in a my app which runs on a week days specified by user on fixed time. Problem here is that my scheduler running for all days instead of running on specified day . here is the code i wrote for this please help to fix this

Calendar calNow = Calendar.getInstance();
                SimpleDateFormat simpDate;
                simpDate = new SimpleDateFormat("kk:mm:ss");
                if(in_Date==1)
                {
                    calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calNow.set(Calendar.MINUTE, minute);
                    calNow.set(Calendar.SECOND, 0);
                    calNow.set(Calendar.MILLISECOND, 0);
                }
            else if(in_Date==2)
                {
                    calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calNow.set(Calendar.MINUTE, minute);
                    calNow.set(Calendar.SECOND, 0);
                    calNow.set(Calendar.MILLISECOND, 0);
                    calNow.set(Calendar.DAY_OF_WEEK,in_SelectedDay);
                }
                etTime.setText(simpDate.format(calNow.getTime()));
                Seconds=calNow.getTimeInMillis();

private void setAlarm(){

    //etTime.setText(simpDate.format(calNow.getTime()));

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

     if(in_Date==1)
     {

       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,alarmManager.INTERVAL_DAY,pendingIntent);
     }
    else if(in_Date==2)
    {

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,1 * 60 * 60 * 1000,pendingIntent);
    }

}
Supreet
  • 2,451
  • 8
  • 25
  • 42
  • 1
    I guess, this has to be taken into account, of course during a reboot or power off these AlarmManager instance will be wipe out, A week is a long time to happen such thing by the owner of the device :) – diyoda_ Sep 11 '14 at 09:43

2 Answers2

17
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY, pendingIntent);

In this line you set the start time to the user selected day, but then set the interval to INTERVAL_DAY.

You should use INTERVAL_DAY * 7 to make sure it repeats on a weekly basis instead:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70
  • there are two cases One for daily in which i have used INTERVAL_DAY and other is for weekly in which i have used 1 * 60 * 60 * 1000 – Supreet Apr 26 '13 at 13:42
  • 1
    The interval is defined in milliseconds. `1 * 60 * 60 * 1000` milliseconds is 1 hour. To get the interval in milliseconds for a week you can use `7 * 24 * 60 * 60 * 1000` or `INTERVAL_DAY * 7` – Leon Lucardie Apr 26 '13 at 14:09
  • 1
    You should access `INTERVAL_DAY` in a `static` way `AlarmManager.INTERVAL_DAY * 7` – Borja Nov 24 '16 at 11:13
5

Is your alarm getting triggered everyday or every hour ?

I am supposing your in_Date is an indicator to choose daily alarm or for specific days .

My idea-> set the alarm for all days, check your day condition in the alarm receiver .

acid_srvnn
  • 693
  • 8
  • 15