Code for setting Alarm Manager to fire on certain day of the week (android)
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));
}
}
}
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.
How about changing the date/time on the phone to make it think it's the day of the alarm?
精彩评论