0

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;
}
user353gre3
  • 2,747
  • 4
  • 24
  • 27
Freephone Panwal
  • 1,547
  • 4
  • 21
  • 39
  • 2
    Use **`adb shell dumpsys alarm`** to see when your alarms are actually scheduled for. – CommonsWare Apr 07 '14 at 20:14
  • One more question. ScheduledWeeklyService class should extend BroadcastReciever or IntentService. As of now, I've this class extending IntentService. Could that be a reason why its not triggering it? – Freephone Panwal Apr 07 '14 at 21:02

0 Answers0