1 回答
TA贡献1982条经验 获得超2个赞
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
的2nd parameter,触发时间触发报警器立即如果calendar.getTimeInMillis()是在过去。所以发生的事情是你可能在晚上 7 点打开应用程序。您希望闹钟在第二天早上 5 点响起,但您calendar.getTimeInMillis()会在同一天早上 5 点响起 。所以你需要为此添加一个检查:
Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis()); // YOU DON'T NEED THIS LINE
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Calendar current = Calendar.getInstance();
int curTime = current.getTimeInMillis();
int alarmTime = calendar.getTimeInMillis();
if (alarmTime >= curTime){
manager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
}else{
calendar.add(Calendar.DAY_OF_MONTH, 1);
int alarmTime = calendar.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
}
添加回答
举报