尝试为一周中的某些日子设置闹钟,但目前很困惑为什么这个闹钟会立即被触发,无论我传给我的是什么时候以及最小... ...(我知道星期几的东西是错了,还没到那个!)
public void setReminder(int hr, int min, int day)
{
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(mContext, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hr);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, day);
// set the alarm to repeat every week at the same time
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, sender);
}
任何想法?
答案 0 :(得分:3)
如果日历calendar.before(Calendar.getInstance()) == true
(这意味着您要设置警报以便在过去启动,它会立即触发。
在这种情况下,您可以执行以下操作:
long start = calendar.getTimeMillis();
if (calendar.before(Calender.getInstance()) {
start += AlarmManager.INTERVAL_DAY * 7;
}
// set alarm with start time
来自docs:
如果过去发生时间,将立即触发警报,警报计数取决于过去触发时间相对于重复间隔的距离。