Reputation: 987
In my project I have a requirement to schedule a task to show notification at a particular time everyday. If I set it for some time today, its triggering and all is going as expected but if the schedule time is tomorrow, its not triggering. Below is my code to set the alarm manager.
public void setMorningRepeatingTask(Context context, int hour, int minutes) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.setAction(Constants.ALARM_MANAGER_INTENT_MORNING_UNIQUE_ACTION);
alarmIntent = PendingIntent.getBroadcast(context, Constants.MORNING_ALARM_UNIQUE_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
if(checkIfTheTimeHasPassed(calendar.getTimeInMillis())){
calendar.add(Calendar.DATE, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
FileLogger.writeToFile("Alarm Set", "Morning repeating Alarm Set");
}
public static boolean checkIfTheTimeHasPassed(long timeInMillis) {
long nowTime = new Date().getTime();
return nowTime > timeInMillis;
}
Kindly help.
Upvotes: 0
Views: 1275
Reputation: 987
Finally I found a solution to the issue I am facing.
I used alarmMgr.set()
method instead of alarmMgr.setRepeating()
and when the alarm rings off I set another alarm for the next day. In this way, it will repeat day after day. :)
I still would like to know whats wrong with alarmMgr.setRepeating()
method. So, if anyone has any idea, please post the solution.
Below is the code I implemented.
public void setMorningRepeatingTask(Context context, int hour, int minutes, boolean forceScheduleNextDay) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.putExtra(Constants.KEY_ALARM_TIME, Constants.VALUE_MORNING_ALARM);
alarmIntent = PendingIntent.getBroadcast(context, Constants.MORNING_ALARM_UNIQUE_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) || forceScheduleNextDay) {
calendar.add(Calendar.DATE, 1);
}
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Log.i("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
FileLogger.writeToFile("Alarm Set", "Morning repeating Alarm Set");
FileLogger.writeToFile("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
}
The above method is for setting the alarm.
if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) {
calendar.add(Calendar.DATE, 1);
}
public static boolean checkIfTheTimeHasPassed(long timeInMillis) {
long nowTime = new Date().getTime();
return nowTime > timeInMillis;
}
The above condition will check if the time has already passed, and if it is passed, it will schedule the alarm for the next day.
When the alarm is triggered, onReceive()
method of the BroadcastReceiver subclass will be called.
@Override
public void onReceive(Context context, Intent intent) {
FileLogger.writeToFile("Alarms rang off!!", "Alarm rang");
//Add your logic here
setMorningRepeatingTask(context,morningTimeCalender.get(Calendar.HOUR_OF_DAY), morningTimeCalender.get(Calendar.MINUTE), true);
}
In this method after doing the things I want on alarm ring, I reschedule the alarm for the next day using same method.
Upvotes: 3