1

I am trying to make an alarm notification every first Thursday of each month.

 am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*30, sender);

I cant use the above code since not all months have 30 days in them, is there a way to accomplish this?

Does this indicate the first Thursday of each month?

cal.set(Calendar.DAY_OF_WEEK, 5); // Thursday
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // Thursday

Thanks

EDIT

private void createThursayScheduledNotification() {
        Calendar calendar3 = Calendar.getInstance();
        calendar3.set(Calendar.DAY_OF_WEEK, 5); // Thursday
        calendar3.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of
                                                            // Each Month
        // Thursday
        calendar3.set(Calendar.HOUR_OF_DAY, 9);
        calendar3.set(Calendar.MINUTE, 0);
        calendar3.set(Calendar.SECOND, 0);
        calendar3.set(Calendar.AM_PM, Calendar.AM);
        AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP,
                calendar3.getTimeInMillis(), pendingIntent3); // every first Thursday
        createThursayScheduledNotification();

    }

2 Answers2

1

Since setRepeating() is no longer exact as of Android 4.4, the best solution will be for you to use setExact() (on Android 4.4) or set() (on Android 4.3 and below). Set an alarm event for the first Thursday of the next month. As part of processing that event, set an alarm for the first Thursday of the next month. And so on.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, but I dont quite get you, could you explain better - Set an alarm event for the first Thursday of the next month. As part of processing that event, set an alarm for the first Thursday of the next month.? – Oluleye IResþekt Idowu Dec 07 '13 at 14:25
  • @OluleyeIResþektIdowu: I do not how to explain it better. Write yourself a `scheduleAnEventForTheFirstThursdayOfNextMonth()` method. Call that once to set up the first event. When that event is triggered, call `scheduleAnEventForTheFirstThursdayOfNextMonth()` to set up the next event. Bear in mind that you will also need to call `scheduleAnEventForTheFirstThursdayOfNextMonth()` after a reboot, probably from a `BOOT_COMPLETED` `BroadcastReceiver`. – CommonsWare Dec 07 '13 at 14:31
  • thank you, I kind of get what you are trying to tell me, I would be glad if you could help me check this question out, its related to this and your id seems to come up in searches related to questions like this. Thank you. I'll appreciate your input - http://stackoverflow.com/questions/20445481/cant-get-alarmmanager-and-multiple-notifications-to-work – Oluleye IResþekt Idowu Dec 07 '13 at 19:20
  • Could you please check my edit, I'm not sure if I got the method right, something tells me it shows the first Thursday of the month and not the First Thursday of NEXT month as you pointed. I will be glad if you can check and make changes. THanks n Good Morning – Oluleye IResþekt Idowu Dec 09 '13 at 01:27
  • @OluleyeIResþektIdowu: "something tells me it shows the first Thursday of the month and not the First Thursday of NEXT month as you pointed" -- whenever creating a `Calendar` object like this, where you use `set()` to put values in specific parts of the date, there is always the risk that you create a date in the past. Usually, I encounter this with people trying to set a particular time in the day, and so checking to see if the date is in the past and adding a day if it is fixes it. You could try the same thing, but adding a month rather than a day, if the date is in the past. – CommonsWare Dec 09 '13 at 12:14
  • thank you, I understand yhu are a very busy person, but could yhu please edit my code or write a code for checking if the day is past or not and then adding a day? It would really help me please. – Oluleye IResþekt Idowu Dec 09 '13 at 12:17
0

You could have a look at this post

How to implement yearly and monthly repeating alarms?

if you are using alarmmanager on at least version 19, you could have a look at setWindow()

Community
  • 1
  • 1
Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52