我正在为大学工作使用Android应用程序,并且试图设置特定日期(或每天)的重复警报。 这是我的代码:
public void createAlarms(List<Reminders> medWreminders) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
for (int i = 0; i < medWreminders.size(); i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar setcalendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, medWreminders.get(i).reminder.getHour());
calendar.set(Calendar.MINUTE, medWreminders.get(i).reminder.getMinutes());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_WEEK, medWreminders.get(i).reminder.getDayInt());
// cancel already scheduled reminders
notificationHelper.cancelNotification(this, medWreminders.get(i).reminder.getId());
if (calendar.before(setcalendar)) {
calendar.add(Calendar.DATE, 1);
}
// enable receiver
ComponentName receiver = new ComponentName(this,
AlertReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
medWreminders.get(i).reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
重要的是要提到,如果警报是每日警报,而不是每天间隔设置一个重复警报,(出于其他原因)我将基于每周间隔设置7种不同的警报-每天间隔一次( ,我认为应该表现相同)。请记住,它也适用于一周中的特定日子。 因此,问题是:如果我设置警报-每天设置一个-在同一天工作,但在第二天什么也没有发生。 有人知道这个问题吗?谢谢!