19

Can somebody give good logic for set repeat days of week alarm? I have done weekly Alarm by using

alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.AM_PM, amorpm);

Long alarmTime = alarmCalendar.getTimeInMillis();

Intent intent = new Intent(Alarm.this, AlarmReciever.class);
intent.putExtra("keyValue", key);
PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, key, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000 , pi); 

The alarm trigger on time and after 7 days it automatically triggers at that time.

But my requirement is I want to choose days rather than just 7 days.

Something like every Monday, Tuesday, Thursday at 9:00 AM - Alarm should trigger automatically. How do I go about doing this in setRepeating?

Can somebody help me out with this?

Thanks!

codersl
  • 2,222
  • 4
  • 30
  • 33
TheDevMan
  • 5,914
  • 12
  • 74
  • 144

4 Answers4

18

These questions talk about the same thing as you want. Those answers will be helpful:

You just need to specify the day to start it and then repeat it every 7 days. There are few ways specified in answers on given questions:

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

Android Notification on specific weekday goes off directly

how to repeat alarm week day on in android

Update:

In your comment you said

How to set the triggerAtMillis part in setRepeating. say for example today is Tuesday, I choose weekly Monday, Wednesday, Friday. - What do I put for Wednesday ?

What I understood that that if today is Tuesday, how to set alarm for lets say Wednesday repeating, right? First of all yes you can use mulltiple id's to set alarms for each day separately.

Then you can add alarmCalendar.set(Calendar.DAY_OF_WEEK, week); line to your existing code. Based on the week day( from 1-7) it repeats for that day. You can pass it into a function as parameter. Like:

    setAlarm(2); //set the alarm for this day of the week

    public void setAlarm(int dayOfWeek) {
        // Add this day of the week line to your existing code
        alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
        alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
        alarmCalendar.set(Calendar.SECOND, 0);
        alarmCalendar.set(Calendar.AM_PM, amorpm);

        Long alarmTime = alarmCalendar.getTimeInMillis();
        //Also change the time to 24 hours.
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 , pi); 
}

I've taken the example from one of above question. Hope its more clear now.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • You mean to say I have to set different ID for different alarm days. But how to set the triggerAtMillis part in setRepeating. say for example today is Tuesday, I choose weekly Monday, Wednesday, Friday. - What do I put for Wednesday trigger 1 day from current time? – TheDevMan Jul 27 '13 at 05:08
  • Yes different ID for different alarms. For your second part, see the updated answer. – Shobhit Puri Jul 27 '13 at 06:46
  • @Shobit Puri I want to set alarm on week days and above example works but how to set weekly like every tuesday, wednesday. If i set trigger time to 7 days then if next day is tuesday then it doesnt work. can you help? – moDev Mar 29 '15 at 13:50
  • Mainly, because i have to set alarm every 1/2/3 weeks on week days so i have to play with trigger time – moDev Mar 29 '15 at 13:55
  • @droid_dev You want to set alarm for each day on weekdays or like every tuesday etc – Shobhit Puri Mar 29 '15 at 19:17
  • It can either each day or every tuesday. Both the conditions can be available. – moDev Mar 29 '15 at 19:28
  • The alarm will trigger immediately if you set an alarm for tomorrow or other day other than today. – Henok Tesfaye Dec 09 '18 at 08:27
  • what does the `am` and `pi` stand for? – confusedstudent Jan 06 '22 at 15:40
6

To set repeat alarm for week days,use below code. Hope this is helpful.

        Calendar calender= Calendar.getInstance();

        calender.set(Calendar.DAY_OF_WEEK, weekNo);  //here pass week number
        calender.set(Calendar.HOUR_OF_DAY, hour);  //pass hour which you have select
        calender.set(Calendar.MINUTE, min);  //pass min which you have select
        calender.set(Calendar.SECOND, 0);
        calender.set(Calendar.MILLISECOND, 0);

        Calendar now = Calendar.getInstance();
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);

        if (calender.before(now)) {    //this condition is used for future reminder that means your reminder not fire for past time
            calender.add(Calendar.DATE, 7);
        }

        final int _id = (int) System.currentTimeMillis();  //this id is used to set multiple alarms

        Intent intent = new Intent(activity, YourServiceClass.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
4
if (Monday.isChecked()) {
                        setalarm(2);
                    } else if (Tuesday.isChecked()) {
                        setalarm(3);
                    } else if (Wednesday.isChecked()) {
                        setalarm(4);
                    } else if (Thursday.isChecked()) {
                        setalarm(5);
                    } else if (Friday.isChecked()) {
                        setalarm(6);
                    } else if (Saturday.isChecked()) {
                        setalarm(7);
                    } else if (Sunday.isChecked()) {
                        setalarm(1);
                    }

public void setalarm(int weekno) {

        cal.set(Calendar.DAY_OF_WEEK, weekno);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,


     cal.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}
Makvin
  • 3,475
  • 27
  • 26
3
Intent intent1 = new Intent(getApplicationContext(),
                            NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(),
                                                          1,
                                                          intent1,
                                                          PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
java.util.Calendar calendar1 = java.util.Calendar.getInstance();

calendar1.set(java.util.Calendar.DAY_OF_WEEK,
              Calendar.MONDAY);
calendar1.set(java.util.Calendar.HOUR_OF_DAY,
              22);
calendar1.set(java.util.Calendar.MINUTE,
              8);
calendar1.set(java.util.Calendar.SECOND,
              0);

alarmManager1.setExact(AlarmManager.RTC, calendar1.getTimeInMillis(), pendingIntent1);
KernelPanic
  • 2,328
  • 7
  • 47
  • 90
VIVEK GAUR
  • 31
  • 1