I am working on a Reminder that sends notification on fixed time to the user.
The alarm is getting off instantly ...
I tried most of the suggestions over stackoverflow
, but still having same issue
Please help me sort this problem out.
server data
user_reminder": [
{
"id": "75",
"name": "Morning Snacks",
"time": "11:00:00",
"days": "1,2,3,4,5,6,7",
"user_id": "14"
},
{
"id": "76",
"name": "Lunch",
"time": "13:00:00",
"days": "1,2,3,4,5,6,7",
"user_id": "14"
},
......
]
My code
for (int i = 0; i < reminderList.size(); i++)
{
String time = reminderList.get(i).getTime(); // "time": "11:00:00"
String strSpit[] = time.split(":");
String strDays[] = reminderList.get(i).getDays().split(","); //"days": "1,2,3,4,5,6,7"
Date date = new Date();
Calendar calNow = Calendar.getInstance();
calNow.setTime(date);
Calendar calAlarm = Calendar.getInstance();
calAlarm.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strSpit[0]));
calAlarm.set(Calendar.MINUTE, Integer.parseInt(strSpit[1]));
for (int j = 0; j < strDays.length; j++)
{
calAlarm.set(Calendar.DAY_OF_WEEK, viewFunctions.getDayInt(strDays[j]));
if (calAlarm.before(calNow))
{
//if its in the past increment
calAlarm.add(Calendar.DATE, 1);
}
notifyIntent.putExtra(Constants.REMINDER_NAME, reminderList.get(i).getName());
pendingIntent = PendingIntent.getBroadcast(this, 0, notifyIntent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calAlarm.getTimeInMillis() , pendingIntent);
}
}
}
Get days : This solves the day numbering
public int getDayInt(String strDay)
{
int dayNumber = 0;
if (strDay.equals("1"))
{
dayNumber = Calendar.MONDAY;
} ......
return dayNumber;
}
screen shot
You alarm is going off instantly because Android will fire off any alarms that are scheduled in the past.
Some of your alarms are getting scheduled in the past because the following code is not working as you expect. Sample code from your question:
if (calAlarm.before(calNow))
{
//if [it's] in the past increment
calAlarm.add(Calendar.DATE, 1);
}
In the above code you are only adding one day to your alarm if the alarm is in the past. So let's say you are running this code on Friday and you read an alarm for Monday. Your code will add one day to the date making it Tuesday, schedule that alarm. The alarm is in the past because Tuesday is still before Friday, so Android will fire off that alarm shortly after being scheduled.
It is unclear from your question what you wish to do with the reminders that are in the past. One possible solution is to schedule them 1 week into the future.
if(calAlarm.before(calNow))
{
// If it's in the past increment by one week.
calAlarm.add(Calendar.DATE, 7);
}
The main problem appears to be with this line:
calAlarm.set(Calendar.DAY_OF_WEEK, viewFunctions.getDayInt(strDays[j]));
What you need to realise is this is just setting the Day of the week which will be displayed in output - It is not changing the underlying date to match, which I think is what you are expecting.
Try using the following code to alter your dates to set an alarm for each day selected:
String strSpit[] = time.split(":");
String strDays[] = reminderList.get(i).getDays().split(","); //"days": "1,2,3,4,5,6,7"
Calendar todayWithTime = Calendar.getInstance(); //setting current time is redundant
todayWithTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strSpit[0]));
todayWithTime.set(Calendar.MINUTE, Integer.parseInt(strSpit[1]));
Calendar alarm;
int today = todayWithTime.get(Calendar.DAY_OF_WEEK);
int offset, target;
for (int j = 0; j < strDays.length; j++) {
alarm = (Calendar) todayWithTime.clone(); //now you have todays date, but correct time
target = strDays[j];
//saturday is biggest day of week
offset = (Calendar.SATURDAY - today + target) % 7; //work out how many days in the future the next occurance of this day is
alarm.add(Calendar.DATE, offset);
... // the rest stays the same
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With