我在这里包含了完整的问题描述,因为我不确定解决方案背后的逻辑是否正确,但我很确定这与我自己设置警报的方式有关,这会导致这种不准确,或者只是有时纯粹的故障(警报根本不会触发)。
用户可以从药物列表中添加新药物。
屏幕一
当找到某种药物时,点击它会显示这个屏幕http://imgur.com/nLC9gTG
该屏幕包含药物的名称,并且在“Posology”标题(绿色条)下可以添加该药物的提醒。
忘记“单位”字段。
“频率”字段接受一个数字,“频率”字段右侧的标签是可点击的,它会导致出现一个下拉菜单,用户可以从中选择“一天的次数”或“每周的次数”。
“星期几”标签(屏幕截图中的标签为空)也是可点击的,它为用户提供了一个下拉菜单,用户可以从中选择一周中的几天。
“治疗持续时间”字段接受一个数字,“治疗持续时间”字段右侧的标签将反映用户选择的“频率”(如果是“每周次数”,则该标签将显示“周”,如果是“每月次”,那么该标签将显示“月”)。
屏幕 2
在第二个屏幕截图http://imgur.com/AcUmlHH - 有一个开关允许用户为他尝试添加的这种药物(项目、实例等)启用提醒。
如果上面的“频率”字段的数字大于 0(例如 2),那么提醒 Switch 将创建一个提醒字段列表,它将显示在“获取通知”绿色条的下方。
当用户最终按下“添加药物”时,将在数据库中创建一个新的药物对象,以及用户选择为此药物对象添加的“频率”(提醒次数)。
创建一个药物表:
id
name
description
dosage
frequency
frequencyType
treatmentDuration
ForeignCollection<MedicationReminder>
ArrayList<DayChoice> (DayChoice is a class with "Day Name" and "Selected")
when
whenString
units
unitForm
remarks
remindersEnabled
创建一个 MedicationReminder 表:
Medication (foreign key for the Medication table)
Calendar
int[] days_of_week
totalTimesToTrigger
在创建这个新的 Medication 对象时:
Medication medication = new Medication();
medication.setFrequency()
medication.setName().setDosage().setRemindersEnabled()....
assignForeignCollectionToParentObject(medication);
assignForeignCollectionToParentObject(药物)
private void assignForeignCollectionToParentObject(Medication medicationObject) {
medicationDAO.assignEmptyForeignCollection(medicationObject, "medicationReminders");
MedicationRemindersRecyclerAdapter adapter =
(MedicationRemindersRecyclerAdapter) remindersRecyclerView.getAdapter();
//Clear previous reminders
medicationObject.getMedicationReminders().clear();
for (int i = 0; i < adapter.getItemCount(); i++) {
int realDaysSelected = 0;
MedicationReminder medReminder = adapter.getItem(i);
medReminder.setMedication(medicationObject);
medReminder.setDays_of_week(daysOfWeekArray);
//These days are populated when the user selected them from the "Days of Week" clickable label
for (int aDaysOfWeekArray : daysOfWeekArray) {
if (aDaysOfWeekArray != 0) realDaysSelected++;
}
medReminder.setTotalTimesToTrigger(
Integer.parseInt(treatmentDurationET.getText().toString()) * realDaysSelected);
medicationObject.getMedicationReminders().add(medReminder);
}
setupMedicationReminders(medicationObject.getMedicationReminders().iterator());
}
setupMedicationReminders()
public void setupMedicationReminders(Iterator<MedicationReminder> medicationRemindersIterator) {
PendingIntent pendingIntent;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
while (medicationRemindersIterator.hasNext()) {
MedicationReminder medReminder = medicationRemindersIterator.next();
for (int i = 0; i < medReminder.getDays_of_week().length; i++) {
int dayChosen = medReminder.getDays_of_week()[i];
if (dayChosen != 0) {
medReminder.getAlarmTime().setTimeInMillis(System.currentTimeMillis());
medReminder.getAlarmTime().set(Calendar.DAY_OF_WEEK, dayChosen);
Intent intent = new Intent(AddExistingMedicationActivity.this, AlarmReceiver.class);
intent.putExtra(Constants.EXTRAS_ALARM_TYPE, "medications");
intent.putExtra(Constants.EXTRAS_MEDICATION_REMINDER_ITEM, (Parcelable) medReminder);
pendingIntent = PendingIntent.getBroadcast(this, medReminder.getId(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
int ALARM_TYPE = AlarmManager.ELAPSED_REALTIME_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(ALARM_TYPE, medReminder.getAlarmTime().getTimeInMillis(),
pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setExact(ALARM_TYPE, medReminder.getAlarmTime().getTimeInMillis(), pendingIntent);
} else {
am.set(ALARM_TYPE, medReminder.getAlarmTime().getTimeInMillis(), pendingIntent);
}
}
}
}
}
问题是当添加药物提醒时,它们总是在添加后不久被触发,并且都是同时触发的。
假设我为周六和周五选择频率 2,治疗持续时间为 1 周。这意味着总共将添加 4 条提醒,周五 2 条,周六 2 条。
当我这样做时,恰好是星期六,警报会在星期六同时触发。
怎么了?