目前我正在开发一个每月设置提醒的应用程序。我无法为我的 alarmmanager 提供正确的重复间隔。请提供相同的信息。 这是我的代码,但这不会在 2 月或有 30 天的月份发出警报。 另外请提供设置每年重复闹钟的代码。
repeatTime=(AlarmManager.INTERVAL_DAY*31);
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi);
谢谢, 沙拉斯
最佳答案
这就是您计算精确一个月后今天之间的间隔的方式,每次触发后使用此逻辑重置警报。即将警报设置为您想要开始的时间点,提供一些未决的 Intent ,一旦警报触发使用以下代码获取下一次触发时间,并再次设置警报以在该时间触发。
private long getDuration(){
// get todays date
Calendar cal = Calendar.getInstance();
// get current month
int currentMonth = cal.get(Calendar.MONTH);
// move month ahead
currentMonth++;
// check if has not exceeded threshold of december
if(currentMonth > Calendar.DECEMBER){
// alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
currentMonth = Calendar.JANUARY;
// Move year ahead as well
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
}
// reset calendar to next month
cal.set(Calendar.MONTH, currentMonth);
// get the maximum possible days in this month
int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// set the calendar to maximum day (e.g in case of fEB 28th, or leap 29th)
cal.set(Calendar.DAY_OF_MONTH, maximumDay);
long thenTime = cal.getTimeInMillis(); // this is time one month ahead
return (thenTime); // this is what you set as trigger point time i.e one month after
}
关于android - 如何设置闹钟每月重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25783777/