android repeating alarm, should repeat on monthly

2019-02-27 11:52发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-02-27 12:19

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

查看更多
Explosion°爆炸
3楼-- · 2019-02-27 12:21

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)
        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);
            }
    }
查看更多
登录 后发表回答