我正在构建一个应用程序,该应用程序将用户的输入作为天,然后设置一周中该天的通知。假设每周的星期三和星期五的下午2点。我已经使用了代码。
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR, hour);
setcalendar.set(Calendar.MINUTE, min);
setcalendar.set(Calendar.SECOND, 0);
if (day=="1") {
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
}
else if (day=="2")
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
}
else if(day=="3")
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
}
else if(day=="4")
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
}
else if(day=="5")
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
}
else if(day=="6")
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
}
else
{
setcalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
}
AlarmManager am = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
setcalendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*7,
pendingIntent);
我每天分别运行上述代码。此代码在 day = Monday 之后运行,然后在 day = Wednesday 运行。
它将设置时间和日期,然后在setInexactRepeating
中使用AlarmManager.INTERVAL_DAY*7
将每条通知设置为每7天出现一次。当我测试代码时,即使“今天是星期一,而用户未选择星期一”,它也会为今天生成通知。