在我的项目中,我需要安排一项任务,以便在每天的特定时间显示通知。如果我今天设置它一段时间,它的触发和所有都按预期进行,但如果计划时间是明天,它不会触发。下面是我设置警报管理器的代码。
public void setMorningRepeatingTask(Context context, int hour, int minutes) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.setAction(Constants.ALARM_MANAGER_INTENT_MORNING_UNIQUE_ACTION);
alarmIntent = PendingIntent.getBroadcast(context, Constants.MORNING_ALARM_UNIQUE_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
if(checkIfTheTimeHasPassed(calendar.getTimeInMillis())){
calendar.add(Calendar.DATE, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
FileLogger.writeToFile("Alarm Set", "Morning repeating Alarm Set");
}
public static boolean checkIfTheTimeHasPassed(long timeInMillis) {
long nowTime = new Date().getTime();
return nowTime > timeInMillis;
}
请帮助。
答案 0 :(得分:2)
最后,我找到了解决我面临的问题的方法。
我使用alarmMgr.set()
方法代替alarmMgr.setRepeating()
,当闹钟响起时,我会在第二天设置另一个闹钟。通过这种方式,它将日复一日地重复。 :)
我仍然想知道alarmMgr.setRepeating()
方法有什么问题。所以,如果有人有任何想法,请发布解决方案。
以下是我实施的代码。
public void setMorningRepeatingTask(Context context, int hour, int minutes, boolean forceScheduleNextDay) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.putExtra(Constants.KEY_ALARM_TIME, Constants.VALUE_MORNING_ALARM);
alarmIntent = PendingIntent.getBroadcast(context, Constants.MORNING_ALARM_UNIQUE_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) || forceScheduleNextDay) {
calendar.add(Calendar.DATE, 1);
}
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Log.i("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
FileLogger.writeToFile("Alarm Set", "Morning repeating Alarm Set");
FileLogger.writeToFile("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
}
以上方法用于设置闹钟。
if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) {
calendar.add(Calendar.DATE, 1);
}
public static boolean checkIfTheTimeHasPassed(long timeInMillis) {
long nowTime = new Date().getTime();
return nowTime > timeInMillis;
}
上述条件将检查时间是否已经过去,如果通过,它将安排第二天的闹钟。
触发警报时,将调用BroadcastReceiver子类的onReceive()
方法。
@Override
public void onReceive(Context context, Intent intent) {
FileLogger.writeToFile("Alarms rang off!!", "Alarm rang");
//Add your logic here
setMorningRepeatingTask(context,morningTimeCalender.get(Calendar.HOUR_OF_DAY), morningTimeCalender.get(Calendar.MINUTE), true);
}
在这个方法完成我想要的警报响铃之后,我使用相同的方法重新安排第二天的警报。