这不是一个理想的解决方案,更像是一种解决方法,但它可以解决问题......
由于我找不到订购接收器的方法,我不得不在 Alarm Receiver 中找到解决方案......所以我创建了一个方法来知道 Alarm 是否有效......它有点大,但它可以处理带有每日和定期警报......这是代码:
public boolean isReceiveAlarmValid (AlertData alertData, AlarmData alarmData){
if ((alertData != null) && (alarmData != null)) {
Calendar caInitialDate = Calendar.getInstance();
//Set beginning of counting to alarm starting date
caInitialDate.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
caInitialDate.set(Calendar.MONTH, (alarmData.getTimeAlarmMonth()) - 1);
caInitialDate.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
caInitialDate.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
caInitialDate.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
caInitialDate.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());
caInitialDate.add(Calendar.MINUTE, alertData.getRescheduleCounter() * GeneralData.getInstance().getAlarmReschedulePeriodicity());
alarmData.setTimeAlarm(GlobalFunctions.generateStringDateFromCalendar(caInitialDate));
//Initialization
int sizeAux = 7;
String auxDateComp = "";
String auxDate = null;
String[] auxDateArray = new String[7];
Calendar calendar = Calendar.getInstance();
int todayDay = calendar.get(Calendar.DAY_OF_WEEK);
if (Consts.FREQUENCY_ONCE == alarmData.getAlarmFrequency()) {
//Alarm Once
auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";
auxDateComp = auxDate;
} else if (Consts.FREQUENCY_DAILY == alarmData.getAlarmFrequency()) {
//Alarm Daily
//Cycle to know what a specific day we are gonna compare on the week
dayCicle:
for (int i = 0; i < 7; i++) {
int auxDay = todayDay + i;
if (auxDay > 7)
auxDay = auxDay - 7;
auxDateArray = new String[7];
if (((alarmData.isRepeatMonday()) && (auxDay == dayMon)) ||
((alarmData.isRepeatTuesday()) && (auxDay == dayTue)) ||
((alarmData.isRepeatWednesday()) && (auxDay == dayWed)) ||
((alarmData.isRepeatThursday()) && (auxDay == dayThu)) ||
((alarmData.isRepeatFriday()) && (auxDay == dayFri)) ||
((alarmData.isRepeatSaturday()) && (auxDay == daySat)) ||
((alarmData.isRepeatSunday()) && (auxDay == daySun))) {
int dayPeriodic = auxDay;
boolean[] isRepeatElements = new boolean[7];
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_SUNDAY] = alarmData.isRepeatSunday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_MONDAY] = alarmData.isRepeatMonday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_TUESDAY] = alarmData.isRepeatTuesday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_WEDNESDAY] = alarmData.isRepeatWednesday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_THURSDAY] = alarmData.isRepeatThursday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_FRIDAY] = alarmData.isRepeatFriday();
isRepeatElements[Consts.ANDROID_DAY_OF_THE_WEEK_SATURDAY] = alarmData.isRepeatSaturday();
int daysCount = i;
Calendar caTimeStartAlarm = Calendar.getInstance();
Calendar caNow = Calendar.getInstance();
//Set beginning of counting to alarm starting date
caTimeStartAlarm.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
caTimeStartAlarm.set(Calendar.MONTH, (alarmData.getTimeAlarmMonth()) - 1);
caTimeStartAlarm.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
caTimeStartAlarm.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
caTimeStartAlarm.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
caTimeStartAlarm.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());
Calendar calInitial = null;
//If time now is after starting time, the base date is the now date
if (caTimeStartAlarm.after(caNow)) {
calInitial = (Calendar) caTimeStartAlarm.clone();
} else {
calInitial = (Calendar) caNow.clone();
}
//cycle that increase days adding to next alarms
for (int z = 0; z < 7; z++) {
//only chosen periodic daily days
if (isRepeatElements[dayPeriodic - 1]) {
Calendar caPossibleAlarm = (Calendar) calInitial.clone();
//Add daily day
caPossibleAlarm.set(Calendar.DAY_OF_MONTH, caPossibleAlarm.get(Calendar.DAY_OF_MONTH) + daysCount);
caPossibleAlarm.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
caPossibleAlarm.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
caPossibleAlarm.set(Calendar.SECOND, alarmData.getTimeAlarmSeconds());
//add a few seconds of margin
caPossibleAlarm.add(Calendar.SECOND, ALARM_RECEIVE_TIME_MARGIN);
if (!(calInitial.after(caPossibleAlarm))) {
//If now is not after (before or equal) next alarm, then it can show
String yearAux = String.format(Locale.getDefault(), "%04d", caPossibleAlarm.get(Calendar.YEAR));
String monthAux = String.format(Locale.getDefault(), "%02d", caPossibleAlarm.get(Calendar.MONTH) + 1);
String dayAux = String.format(Locale.getDefault(), "%02d", caPossibleAlarm.get(Calendar.DAY_OF_MONTH));
auxDate = yearAux + "-" + monthAux + "-" + dayAux + " " +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";
auxDateComp = auxDate;
}
}
if ("".equals(auxDateComp)) {
dayPeriodic++;
if (dayPeriodic > 7)
dayPeriodic = dayPeriodic - 7;
daysCount++;
} else {
//it has found a value
break dayCicle;
}
}
break dayCicle;
}
}
} else {
//Periodic Weekly / Monthly
auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";
Calendar caNow = Calendar.getInstance();
Calendar caAlarmTime = Calendar.getInstance();
caAlarmTime.set(Calendar.YEAR, alarmData.getTimeAlarmYear());
caAlarmTime.set(Calendar.MONTH, alarmData.getTimeAlarmMonth());
caAlarmTime.set(Calendar.DAY_OF_MONTH, alarmData.getTimeAlarmDay());
caAlarmTime.set(Calendar.HOUR_OF_DAY, alarmData.getTimeAlarmHour());
caAlarmTime.set(Calendar.MINUTE, alarmData.getTimeAlarmMinute());
if (caAlarmTime.after(caNow)) {
//next date is from the alarm
auxDate = String.format(Locale.getDefault(), "%04d", alarmData.getTimeAlarmYear()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMonth()) + "-" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmDay()) + " " +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmHour()) + ":" +
String.format(Locale.getDefault(), "%02d", alarmData.getTimeAlarmMinute()) + ":00";
} else {
//It needs to check when is the next possible alarm
boolean foundNextDate = false;
foundNextDateCycle:
while (foundNextDate == false) {
if (Consts.FREQUENCY_WEEKLY == alarmData.getAlarmFrequency())
caAlarmTime.add(Calendar.DAY_OF_MONTH, 7);
else if (Consts.FREQUENCY_MONTHLY == alarmData.getAlarmFrequency())
caAlarmTime.add(Calendar.MONTH, 1);
else
caAlarmTime.add(Calendar.YEAR, 1);
try {
if (caAlarmTime.after(caNow))
foundNextDate = true;
} catch (Exception e) {
break foundNextDateCycle;
}
}
auxDate = String.format(Locale.getDefault(), "%04d", caAlarmTime.get(Calendar.YEAR)) + "-" +
String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.MONTH) + 1) + "-" +
String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.DAY_OF_MONTH)) + " " +
String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.HOUR_OF_DAY)) + ":" +
String.format(Locale.getDefault(), "%02d", caAlarmTime.get(Calendar.MINUTE)) + ":00";
}
auxDateComp = auxDate;
}
if("".equals(auxDateComp))
return false;
Calendar calendarNow = Calendar.getInstance();
Calendar calendarBegin = Calendar.getInstance();
Calendar calendarFisnish = Calendar.getInstance();
int marginMinutes = 1; //1 minute of margin
calendarBegin.set(Calendar.YEAR, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "yyyy", auxDateComp)));
calendarBegin.set(Calendar.MONTH, (Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "MM", auxDateComp))) - 1);
calendarBegin.set(Calendar.DAY_OF_MONTH, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "dd", auxDateComp)));
calendarBegin.set(Calendar.HOUR_OF_DAY, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "HH", auxDateComp)));
calendarBegin.set(Calendar.MINUTE, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "mm", auxDateComp)));
calendarBegin.set(Calendar.SECOND, Integer.parseInt(GlobalFunctions.formateDateFromString("yyyy-MM-dd HH:mm:ss", "ss", auxDateComp)));
calendarFisnish = (Calendar) calendarBegin.clone();
calendarBegin.set(Calendar.MINUTE, calendarBegin.get(Calendar.MINUTE) - marginMinutes);
calendarFisnish.set(Calendar.MINUTE, calendarFisnish.get(Calendar.MINUTE) + marginMinutes);
if (calendarNow.after(calendarBegin) && calendarNow.before(calendarFisnish)) {
Log.e(GlobalFunctions.getTag(), "Gonna show alarm!");
return true;
} else {
Log.e(GlobalFunctions.getTag(), "NOT Gonna show alarm!");
return false;
}
} else{
//alarmData or alertData are NULL
return false;
}
}
在此之后,我还检查我收到的警报数组是否具有相同 ID 的值,然后只显示其中一个(只是为了确保没有剩菜)
2 年后我尝试设置小时,而且速度非常快(至少在我的设备上)
同样,这不是一个理想的解决方案,如果有人能找到更好的解决方案,请发布