I have an app that sends periodic alarms every day of the week, through the method setRepeating
from AlarmManager
:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(INTENT_TAG_ALERT_ID, lastAlertId.getId()+"");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cDate.getTimeInMillis(), ((AlarmManager.INTERVAL_DAY)*7), pendingIntent);
And then get the alarm on my AlarmReceiver
defined on manifest as a Receiver:
<receiver
android:name=".manager.alarm.AlarmReceiver"
android:exported="true">
</receiver>
The problem is when the user changes the Date/Time from his phone for a day or 2 in the future, I receive several calls from the receiver (depending on the number of days chosen). Since I use a setRepeating
method I can't validate if the alarm was sent at the right time.
I know this is normal procedure as described on here: "A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.", but I want to prevent it.
I thought of putting a listener to know when the user changes the date and time of his phone, to unschedule all the alarms and then schedule them again, as you can see bellow:
public class DateTimeChangedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED)) {
AlarmScheduler.unscheduleAllAlarms();
//and then
AlarmScheduler.scheduleAllAlarms();
}
}
}
And the manifest:
<receiver android:name=".manager.alarm.DateTimeChangedBroadcastReceiver"
android:priority="1000">
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.ACTION_TIME"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
</intent-filter>
</receiver>
But in this solution, the receiver of time changes come after the receiver from the alarm, so it doesn't serve me much...
Is there a way to choose the order of my receivers?
Is there a better way to do this?
I also though of putting a listener to clock time, to store it. if the user changes the hour I should only validate the difference... but I believe I can get a better way to do this (preferentially not involving another listener on the app)
This isn't an ideal solution, more like a workaround, but it does the trick...
Since I couldn't find a way to order my receivers, I was forced to find a solution inside Alarm Receiver... so I created a method to know if Alarm is valid or not... it's a bit large, but it deals with daily and periodic alarms... Here's the code:
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;
}
}
After this I also check if my array of received alarms, has values with the same Id, and then only show one of them (just to make sure that there's no leftovers)
I tried set the Hour 2 years later, and it's pretty fast (at least on my device)
Again, this is not a ideal solution, if someone would find a better solution, please post it