简体   繁体   中英

if and broadcastreceiver

In a BroadcastReceiver class I've this code but I don't understand why first notify does not run. If I remove if cycle, alarmmanager runs perfectly. Also values are ok (I've a toast that show me it). Is a "if" problem?

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);
    }
    }

You should complete your if 's with else . Log errors if you hit the else 's

Also check all the conditions in your if 's, eg what is the value of notify and CheckBoxPreference. This should help you to debug the code yourself.

   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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM