亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Toast 消息在 Android 通知設置期間顯示兩次

Toast 消息在 Android 通知設置期間顯示兩次

拉莫斯之舞 2023-04-26 15:37:17
我在 Android 應用程序中有一個選項可以設置兩個通知?;旧希斢脩魡螕舭粹o時,將顯示一個時間選擇器,在第一次選擇器完成后,將彈出第二個時間選擇器供用戶第二次插入。兩個時間選擇器都在不同的方法中,在第一種方法結束時顯示吐司,與第二種方法相同。問題是當第一次選擇器完成時,兩條吐司消息立即觸發,然后當用戶第二次完成選擇器時,第二條吐司消息再次觸發。我已經包含了下面的代碼。 /** * 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();}
查看完整描述

3 回答

?
白板的微信

TA貢獻1883條經驗 獲得超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 語句”,如下所示:


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

    }

}


查看完整回答
反對 回復 2023-04-26
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

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

    }

}


查看完整回答
反對 回復 2023-04-26
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

塊內發現邏輯錯誤


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

    }

}


查看完整回答
反對 回復 2023-04-26
  • 3 回答
  • 0 關注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號