Reputation: 43
I have an option in the android app to set up two notifications. Basically when the user clicks the button a time picker will show, after the first time picker is finished a second one will pop up for the user to insert the second time.
Both time pickers are in seperate methods, at the end of the first method a toast is displayed, same as the second method. The issue is when the first time picker finishes both toast messages fire immediately, then when the user finishes the second time picker the 2nd toast message fires again. I have included the code below.
/**
* Method which sets the first daily notification
*/
private void startTwiceDailyNotification(Calendar c) {
DialogFragment timePicker = new TimePickerFragment();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "First Notification Set.", Toast.LENGTH_SHORT).show();
timePicker.show(getSupportFragmentManager(), "time picker4");
hasSelected = 2;
}
/**
* Method which sets the second daily notification
*/
private void startTwiceDailyNotification2(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 2, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Second Notification Set.", Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if (hasSelected == 1) {
startTwiceDailyNotification(calendar);
}
if (hasSelected == 2) {
startTwiceDailyNotification2(calendar);
}
}
Upvotes: 0
Views: 287
Reputation: 4658
Logical error found inside the block
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if (hasSelected == 1) {
//At this point hasSelected is 1
startTwiceDailyNotification(calendar);
//At this point hasSelected is 2
}
//No **else** statement so the next if statement is also checked
//Use an else here to prevent this block from executing when previous is true
if (hasSelected == 2) {
//Next code executed also
startTwiceDailyNotification2(calendar);
}
}
Upvotes: 0
Reputation: 824
Like you can read in this answer.:
When you write multiple if statements, it's possible that more than one of them will be evaluated to true, since the statements are independent of each other.
When you write a single if else-if else-if ... else statement, only one condition can be evaluated to true (once the first condition that evaluates to true is found, the next else-if conditions are skipped).
So in your example after method startTwiceDailyNotification
, variable hasSelected
is set to 2. So the second "if statement" is evaluated to true and this is why method startTwiceDailyNotification2
is called.
To fix it you should use "a single if else-if else-if ... else statement", like this:
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if (hasSelected == 1) {
startTwiceDailyNotification(calendar);
}
else if (hasSelected == 2) {
startTwiceDailyNotification2(calendar);
}
}
Upvotes: 1
Reputation: 1957
hasSelected = 2;
is marked that's why it shows up . First set hasselected =1
when button is clicked.
And try this method :
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if (hasSelected == 1) {
startTwiceDailyNotification(calendar);
} else
if (hasSelected == 2) {
startTwiceDailyNotification2(calendar);
}
}
Upvotes: 0