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!
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With