Android 中频和广播接收机

Android 中频和广播接收机,android,Android,在BroadcastReceiver类中,我有这段代码,但我不明白为什么FirstNotify不运行。如果我删除If循环,alarmmanager将正常运行。另外,价值观也可以(我有一个祝酒词向我展示它)。是否存在“如果”问题 您应该用else完成if。如果您点击了else,则记录错误 还要检查if中的所有条件,例如notify和CheckBoxPreference的值是多少。这将帮助您自己调试代码 if (CheckboxPreference){ if (notify.e

在BroadcastReceiver类中,我有这段代码,但我不明白为什么FirstNotify不运行。如果我删除If循环,alarmmanager将正常运行。另外,价值观也可以(我有一个祝酒词向我展示它)。是否存在“如果”问题


您应该用
else
完成
if
。如果您点击了
else
,则记录错误

还要检查
if
中的所有条件,例如
notify
和CheckBoxPreference的值是多少。这将帮助您自己调试代码

   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

    }

您的代码似乎没有问题。但是,您可以尝试
if(notify.trim().equals(…)
,或者您是否以某种方式验证了您正在进入
if()
块?
   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

    }