gpt4 book ai didi

java - Android 通知设置期间 Toast 消息显示两次

转载 作者:行者123 更新时间:2023-12-02 09:39:05 25 4
gpt4 key购买 nike

我在 Android 应用程序中有一个选项可以设置两个通知。基本上,当用户单击按钮时,会显示一个时间选择器,第一次时间选择器完成后,会弹出第二个时间选择器,供用户第二次插入。

两个时间选择器都采用单独的方法,在第一个方法结束时会显示一个 toast,与第二个方法相同。问题是,当第一次选择器完成两个 toast 消息时立即触发,然后当用户完成第二次选择器时,第二个 toast 消息再次触发。我已包含以下代码。

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

最佳答案

就像你可以在这个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).

因此,在方法startTwiceDailyNotification之后的示例中,变量hasSelected设置为2。因此第二个“if语句”被评估为true,这就是方法的原因>startTwiceDailyNotification2 被调用。

要修复它,您应该使用“单个 if else-if else-if ... else 语句”,如下所示:

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

关于java - Android 通知设置期间 Toast 消息显示两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57250995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com