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

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


两个时间选择器都在不同的方法中,在第一种方法结束时显示吐司,与第二种方法相同。问题是当第一次选择器完成时,两条吐司消息立即触发,然后当用户第二次完成选择器时,第二条吐司消息再次触发。我已经包含了下面的代码。


 /**

 * 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();


}


拉莫斯之舞
浏览 197回答 3
3回答

白板的微信

就像您可以阅读此答案一样。:当您编写多个 if 语句时,可能会有多个 if 语句被评估为 true,因为这些语句彼此独立。当您编写单个 if else-if else-if ... else 语句时,只能将一个条件评估为 true(一旦找到评估为 true 的第一个条件,将跳过下一个 else-if 条件)。因此,在您的示例中,在 method 之后startTwiceDailyNotification,变量hasSelected设置为 2。因此第二个“if 语句”被评估为 true,这就是startTwiceDailyNotification2调用 method 的原因。要修复它,您应该使用“一个 if else-if else-if ... else 语句”,如下所示:@Overridepublic 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);    }}

DIEA

hasSelected = 2;被标记这就是它出现的原因。hasselected =1单击按钮时首先设置。试试这个方法:@Overridepublic 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);    }}

扬帆大鱼

块内发现逻辑错误@Overridepublic 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);    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java