【问题标题】:Toast Message displaying twice during Android Notification Set Up在 Android 通知设置期间显示两次 Toast 消息
【发布时间】:2019-12-06 15:01:22
【问题描述】:

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

【问题讨论】:

  • hasSelected = 2; 设置为 2,因此调用了第二个。

标签: java android


【解决方案1】:

在块内发现逻辑错误

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

【讨论】:

    【解决方案2】:

    就像你可以阅读这个answer.

    当您编写多个 if 语句时,可能会超过 其中之一将被评估为真,因为这些陈述是 相互独立。

    当你写一个 if else-if else-if ... else 语句时,只有 一个条件可以被评估为真(一旦第一个条件 计算结果为真,则跳过下一个 else-if 条件)。

    因此,在您的示例中,在方法 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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      hasSelected = 2; 被标记,这就是它出现的原因。单击按钮时首先设置hasselected =1

      试试这个方法:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-09
        • 2014-07-04
        • 1970-01-01
        • 1970-01-01
        • 2012-07-21
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多