1 回答

TA貢獻1982條經(jīng)驗 獲得超2個贊
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
的2nd parameter,觸發(fā)時間觸發(fā)報警器立即如果calendar.getTimeInMillis()是在過去。所以發(fā)生的事情是你可能在晚上 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);
}
添加回答
舉報