2

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 31 days, as we don't have same number of days for each month I'am having trouble with figuring out the interval of this alarm, please help me how to figure out what this INTERVAL_Value has to be or how to handle it in another way

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_Value, alarmIntent);
4

2 回答 2

1

我们可以从源接收 currentMonth 值,它是一个整数

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)
        GregorianCalendar cal = (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);
            }
    }
于 2014-10-30T06:00:10.947 回答
0

你应该使用cal.getActualMaximum(Calendar.DAY_OF_MONTH));. 无论月份如何,这都会为您提供每个月的最大日期。

于 2017-01-28T13:41:39.847 回答