0

在 BroadcastReceiver 类中,我有这段代码,但我不明白为什么第一次通知不运行。如果我删除 if 循环,alarmmanager 会完美运行。价值观也还可以(我有一个向我展示的祝酒词)。是“如果”问题吗?

CheckboxPreference = prefs.getBoolean("checkboxPref", true);
    notify = prefs.getString("notify", "");

if (CheckboxPreference){
        if (notify.equals("1")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
        }
        if (notify.equals("2")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);
        }
        if (notify.equals("3")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, middleAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);
    }
    }
4

1 回答 1

1

你应该ifelse. else如果您点击's ,则记录错误

还要检查您的所有条件,例如CheckBoxPreferenceif的值是什么。notify这应该可以帮助您自己调试代码。

   if (CheckboxPreference){
        if (notify.equals("1")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
        }

        else if (notify.equals("2")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);
        }
        else if (notify.equals("3")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, middleAlarm);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);
        }
        else {
           // Log something
        }
    }  
    else {
       // Log CheckBoxPref false

    }
于 2012-12-31T14:59:09.097 回答