Can somebody give good logic for set repeat days of week alarm? I have done weekly Alarm by using
alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt
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);
}
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);
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);