I am trying to invoke a scheduler every Tuesday at 11 AM but somehow below code is not working.
Using Calendar object, I'm getting the next date to be scheduled & then setting it up in scheduler.
Can someone point me what error exists in below code?
private static final int PERIOD = 7 * 24 * 60 * 60 * 1000;
DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.ENGLISH);
Calendar c = nextDayOfWeek(Calendar.TUESDAY); // Set the day of week at which you want to trigger weekly task
c.set(Calendar.HOUR_OF_DAY, 11); // Set the time at which you want to trigger weekly task
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
boolean weeklyAlarmUp = (PendingIntent.getBroadcast(ctxt, 0, new Intent(ctxt, ScheduledWeeklyService.class), PendingIntent.FLAG_NO_CREATE) != null);
if (!weeklyAlarmUp) {
Intent i = new Intent(ctxt, ScheduledWeeklyService.class);
PendingIntent piWeekly = PendingIntent.getBroadcast(ctxt, 0, i, 0);
AlarmManager alarmMgrWeekly = (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
alarmMgrWeekly.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), PERIOD, piWeekly);
}
My Utility method is as shown below:
public static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (!(diff > 0)) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}