我尝试设置Android应用。这必须在周五下午15.00 /下午3点每周给我证明。此时每天设置通知都没有问题,但即使不是在特定的日期和时间。
我希望你找到我的错误并给我一些提示。这是我的代码:
pip3 install numpy
谢谢你
添
答案 0 :(得分:1)
对于重复警报(setRepeating,setInexactRepeating),您应该在将来设置时间(如果您指定的触发时间是过去的,则警报立即触发)。因此,如果您在每个星期五首先需要警报,那么您应该在下周五计算:
private void alarmMethod() {
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
// get current time
Calendar currentTime = Calendar.getInstance();
// setup time for alarm
Calendar alarmTime = Calendar.getInstance();
// set time-part of alarm
alarmTime.set(Calendar.SECOND, 0);
alarmTime.set(Calendar.MINUTE, 0);
alarmTime.set(Calendar.HOUR, 3);
alarmTime.set(Calendar.AM_PM, Calendar.PM);
alarmTime.set(Calendar.DAY_OF_WEEK, Calendar.Friday);
// check if it in the future
if (currentTime.getTimeInMillis() < alarmTime.getTimeInMillis()) {
// nothing to do - time of alarm in the future
} else {
int dayDiffBetweenClosestFriday = (7 + alarmaTime.get(Calendar.DAY_OF_WEEK) - calendar.get(Calendar.DAY_OF_WEEK)) % 7;
if (dayDiffBetweenClosestFriday == 0) {
// Today is Friday, but current time after 3pm, so schedule for the next Friday
dayDiffBetweenClosestFriday = 7;
}
alarmTime.add(Calendar.DAY_OF_MONTH, dayDiffBetweenClosestFriday);
}
// calculate interval (7 days) in ms
int interval = 1000 * 60 * 60 * 24 * 7;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), interval, pendingIntent);
}
如果用户重新启动设备,您还应该自动重启重复警报(默认情况下,当设备关闭时取消所有警报):