Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // Tue, Wed...
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*24*60*60*1000, pi);
this will fire once a week at monday but: I would like to set pending alarm intent only at weekdays (without saturday and sunday), how to do it only by using one intent ? I don't want to use 5 different PendingIntents with 5 different ID's, because then I have to cancel 5
Maybe:
Create setAlarm method:
public void setAlarm(long interval){
Calendar calendar = Calendar.getInstance();
//Alarm will go off at 1
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pi);
}
Create cancelAlarm method (optional, if you want to use it somewhere):
public void cancelAlarm(Context context){
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context,MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
}
check the day in your onReceive()
@Override
public void onReceive(Context context, Intent intent) {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK); //Sunday = 1, Saterday = 7
switch(day){
case 6: //Friday
long interval = 3*24*60*60*1000 //skip saterday and sunday
setAlarm(interval);
case 2: //Monday
setAlarm(AlarmManager.INTERVAL_DAY); //Alarm will repeat everyday, until friday
break;
}
PS: Don't forget to set your alarm somewhere for the first time using: setAlarm(AlarmManager.INTERVAL_DAY);
I think this should work.