android alarmmanager在指定日期每个月重复一次

时间:2013-07-17 10:27:37

标签: android alarmmanager

我正在开发一个可以在指定时间内发送短信的应用程序,我已经添加了一些功能,但是当我添加可以在每个月发送短信的功能时我感到难过,我怎么能做那个功能?

我在link上尝试了答案代码,但没有成功。

我认为问题出在AlarmManager类的setRepeating函数中的interval参数

mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), interval , pendingIntent);

变量间隔的正确值是什么?

2 个答案:

答案 0 :(得分:1)

此处间隔是两个警报之间的时间(以毫秒为单位)。

 //e.g
long interval=5*60*1000;

mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), interval , pendingIntent);

然后我的闹钟将在5分钟后重复。

修改

 int days=GetTotalDays(current_month);
    interval=(days)*24*60*60*1000;

    public int GetTotalDays(int current_month)
    {
    //here u can fetch current months total days
    //suppose current month is 6(means july as it starts from 0)
    //& u want to set alarm to next month(august)
    //so get remaining days from calender of current month  +  day of next month
    //e.g(14-7 to 14-8)  so 
    //remaining days from calender of current month = 18(14-7 to 31-7)
    //day of next month =14.
    //so return would be (18+14-2=30).(-2.as it takes currentdate and nextdate also in   calculation)
int currentdate=14;
int nextdate=14;
int totalDays=getDaysInMonthInPresentYear(6);
int myDays=(totalDays-currentdate)+nextdate;
return myDays-2;

    }


public static int getDaysInMonthInPresentYear(int monthNumber)
 {
 int days=0;
 if(monthNumber>=0 && monthNumber<12){
 try
 {
 Calendar calendar = Calendar.getInstance();
 int date = 1;
 int year = calendar.get(Calendar.YEAR);
 calendar.set(year, monthNumber, date);
 days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 } catch (Exception e)
 {
 if(e!=null)
 e.printStackTrace();
 }
 }
 return days;
 }

答案 1 :(得分:0)

alarmManager .setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    calendar1.getTimeInMillis() + AlarmManager.INTERVAL_DAY*30,
                    AlarmManager.INTERVAL_DAY*30, pendingIntent);

您可以尝试这样