在我的闹钟应用程序中,我有以下功能来计算闹钟的触发时间,如果闹钟时间> 1,则其工作完美。当前时间,但是如果闹钟时间通过“今天设定时间过去”当前时间>警报应该在明天发出警报的时间,但是在我的功能中它会在几小时或几分钟后根据差异数量在同一天发射。我该如何解决这个问题?如果警报设定时间过去,我应该在24小时后到明天等待添加到我的功能的条件是什么?请帮帮我
private long getTriggerTime(int hour,int minute)
{
long triggerTime = System.currentTimeMillis() ;
Calendar calendarNow = Calendar.getInstance();
int currentHour = calendarNow.get(Calendar.HOUR_OF_DAY);
int currentMinute = calendarNow.get(Calendar.MINUTE);
int HourDifference = currentHour-hour;
int MinuteDifference = currentMinute -minute;
if(HourDifference < 0 ){ HourDifference = HourDifference*(-1); }
if(MinuteDifference < 0){ MinuteDifference = MinuteDifference*(-1); }
triggerTime = triggerTime + ( HourDifference*60*60*1000) + (MinuteDifference*60*1000 );
return triggerTime;
}
这里我如何使用此功能
am.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerTime(hour,min),AlarmManager.INTERVAL_DAY ,sender);
注意:24小时格式的小时,分钟
答案 0 :(得分:2)
这是警报时间的示例代码,如果警报时间>当前时间,否则它将持续24小时.....
private long getAlarmTimeInMillis(int hour,int minute){
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);//
calendar.set(Calendar.SECOND, 0);
if (calendar.getTimeInMillis() < System
.currentTimeMillis()) {
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+ 1);
// add 24 hours to the calender.getTimeMillis
}
return calendar.getTimeInMillis();
}
试试这个......
答案 1 :(得分:0)
使用此:
Calendar calendarNow = Calendar.getInstance();
calendarNow.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH),
calendarNow.get(Calendar.DAY_OF_MONTH),
calendarNow.get(Calendar.HOUR_OF_DAY),
calendarNow.get(Calendar.MINUTE), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarNow.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);