Skip to content Skip to sidebar Skip to footer

Android Repeating Alarm, Should Repeat On Monthly Basis And On Same Day For Each Month So On After Its Set

alarm should repeat on monthly basis, once a month on same date for each month so on after its set like if i place alarm on october 31, then it should repeat on 31 of months having

Solution 1:

we can recieve currentMonth value from source and it is an integer

if (currentMonth == Calendar.JANUARY || currentMonth == Calendar.MARCH || currentMonth == Calendar.MAY || currentMonth == Calendar.JULY 
            || currentMonth == Calendar.AUGUST || currentMonth == Calendar.OCTOBER || currentMonth == Calendar.DECEMBER){
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31, alarmIntent);
    }
    if (currentMonth == Calendar.APRIL || currentMonth == Calendar.JUNE || currentMonth == Calendar.SEPTEMBER 
            || currentMonth == Calendar.NOVEMBER){
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 30, alarmIntent);
        }


    if  (currentMonth == Calendar.FEBRUARY){//for feburary month)GregorianCalendarcal= (GregorianCalendar) GregorianCalendar.getInstance();    
            if(cal.isLeapYear(year)){//for leap year feburary month  
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 29, alarmIntent);
            }
            else{ //for non leap year feburary month
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28, alarmIntent);
            }
    }

Solution 2:

You should use cal.getActualMaximum(Calendar.DAY_OF_MONTH));. This gives you the maximum date for each month irrespective of the month.

Post a Comment for "Android Repeating Alarm, Should Repeat On Monthly Basis And On Same Day For Each Month So On After Its Set"