安卓alarmmanager重复每月在指定日期 [英] android alarmmanager repeat every month in specified date

查看:542
本文介绍了安卓alarmmanager重复每月在指定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发可在指定的时间发送短信的应用程序,我有添加一些关于它的功能,但同时我补充一点,在每月14日发短信功能我难倒,我怎么能作出这样的功能?

I'm developing an application that can send a SMS in specified time, I have add some function on it, but i get stumped while i add function that can send a sms in every month on 14th, how can i make that function?

我已经尝试对这个答案code <一个href=\"http://stackoverflow.com/questions/13227035/android-repeat-alarm-for-every-monday-or-every-tuesday\">link但没有奏效。

i have try the answer code on this link but didn't work.

我presume问题是在AlarmManager类setRepeating功能

I presume the problem is on the interval parameter in setRepeating function of AlarmManager class

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

什么变量区间的正确价值?

what the proper value of variable interval?

推荐答案

在这里间隔为两个报警之间毫秒的时间。

here interval is time in milliseconds between two alarms.

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

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

然后我会报警5分钟后重复每一个。

then my alarm will repeat every after 5 mins.

修改

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

这篇关于安卓alarmmanager重复每月在指定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆