Reputation: 861
So I have written this code for getting AlarmManager to fire on a specified day of the week. Trouble is, I can't really sit around and wait for the notification to pop up tomorrow to see if I got the code right! Can someone take a look and see if what I wrote makes sense please?
(day is an int (1-7 to represent Sunday-Saturday))
if(calendar.get(Calendar.DAY_OF_WEEK) != day)
{
if(day > calendar.get(Calendar.DAY_OF_WEEK))
{
calendar.add(Calendar.DAY_OF_MONTH, day - calendar.get(Calendar.DAY_OF_WEEK));
}
if(day < calendar.get(Calendar.DAY_OF_WEEK))
{
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
if(day > 1)
{
calendar.add(Calendar.DAY_OF_MONTH, 7 - (calendar.get(Calendar.DAY_OF_WEEK) - day));
}
}
}
Upvotes: 0
Views: 806
Reputation: 52956
Write a unit test for your code and test it with different dates, days of the week. Then you can be sure that you are calculating the alarm fire time correctly. Any other problems would be with the AlarmManager itself, which is outside of your control.
Upvotes: 1
Reputation: 80340
How about changing the date/time on the phone to make it think it's the day of the alarm?
Upvotes: 0