2

我已安排在每天上午 10 点触发警报。

我在星期一上午 12 点设置这个闹钟。

如果警报时间已过,则警报将立即触发。

但出于我的要求,我不想立即触发。我可以在第二天上午 10 点触发。

以下是我当前设置警报的代码:

Calendar activeModeTime = Calendar.getInstance();
    // activeModeTime.setTimeZone(TimeZone.getTimeZone(Constants.TIME_ZONE));
    activeModeTime.set(Calendar.HOUR_OF_DAY,
            mSharedPrefManager.getActiveStartHourPref());
    activeModeTime.set(Calendar.MINUTE,
            Constants.DEFAULT_ACTIVE_START_MINUTE);
    activeModeTime.set(Calendar.SECOND,
            Constants.ALL_START_END_DEFAULT_SECOND);

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            activeModeTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            getActiveModeAlarmPendingIntent());

我该如何做到这一点。

4

2 回答 2

5

检查您设置的时间是否是过去,如果是,则将 24 小时添加到您的触发时间。

if(activeModeTime < System.currentTimeMillis()){
    activeModeTime += AlarmManager.INTERVAL_DAY;
}
于 2015-04-06T11:09:03.393 回答
0

写间隔而不是 REPEATING_INTERVAL。对于上述上午 10 点的示例,您需要在下面的代码中编写 AlarmManager.INTERVAL_DAY 而不是 REPEATING_INTERVAL。

if(Calendar.getInstance().getTimeInMillis()>=calendar.getTimeInMillis()){
   timeInMillis = calendar.getTimeInMillis() + REPEATING_INTERVAL;
}else { 
   timeInMillis = calendar.getTimeInMillis();
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis,REPEATING_INTERVAL, pendingIntent);
于 2017-09-19T19:37:15.877 回答