How to set alarm to first Thursday of each month using AlarmManager

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();

    }

Upvotes: 1

Views: 1461

Answers (2)

Basile Perrenoud
Basile Perrenoud

Reputation: 4112

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()

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006654

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.

Upvotes: 1

Related Questions