0

我正在开发一个可以在指定时间发送短信的应用程序,我已经添加了一些功能,但是当我添加可以在每月 14 日发送短信的功能时,我被难住了,我该如何制作该功能?

我已尝试此链接上的答案代码,但没有奏效。

我认为问题出在 AlarmManager 类的 setRepeating 函数中的间隔参数上

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

可变区间的正确值是多少?

4

2 回答 2

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;
 }
于 2013-07-17T10:32:49.507 回答
0
alarmManager .setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    calendar1.getTimeInMillis() + AlarmManager.INTERVAL_DAY*30,
                    AlarmManager.INTERVAL_DAY*30, pendingIntent);

你可以这样尝试

于 2013-11-29T10:31:19.467 回答