第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Toast 消息在 Android 通知設(shè)置期間顯示兩次

Toast 消息在 Android 通知設(shè)置期間顯示兩次

拉莫斯之舞 2023-04-26 15:37:17
我在 Android 應(yīng)用程序中有一個(gè)選項(xiàng)可以設(shè)置兩個(gè)通知?;旧?,當(dāng)用戶單擊按鈕時(shí),將顯示一個(gè)時(shí)間選擇器,在第一次選擇器完成后,將彈出第二個(gè)時(shí)間選擇器供用戶第二次插入。兩個(gè)時(shí)間選擇器都在不同的方法中,在第一種方法結(jié)束時(shí)顯示吐司,與第二種方法相同。問題是當(dāng)?shù)谝淮芜x擇器完成時(shí),兩條吐司消息立即觸發(fā),然后當(dāng)用戶第二次完成選擇器時(shí),第二條吐司消息再次觸發(fā)。我已經(jīng)包含了下面的代碼。 /** * 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貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

就像您可以閱讀此答案一樣。:


當(dāng)您編寫多個(gè) if 語句時(shí),可能會(huì)有多個(gè) if 語句被評(píng)估為 true,因?yàn)檫@些語句彼此獨(dú)立。


當(dāng)您編寫單個(gè) if else-if else-if ... else 語句時(shí),只能將一個(gè)條件評(píng)估為 true(一旦找到評(píng)估為 true 的第一個(gè)條件,將跳過下一個(gè) else-if 條件)。


因此,在您的示例中,在 method 之后startTwiceDailyNotification,變量hasSelected設(shè)置為 2。因此第二個(gè)“if 語句”被評(píng)估為 true,這就是startTwiceDailyNotification2調(diào)用 method 的原因。


要修復(fù)它,您應(yīng)該使用“一個(gè) 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);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-26
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊

hasSelected = 2;被標(biāo)記這就是它出現(xiàn)的原因。hasselected =1單擊按鈕時(shí)首先設(shè)置。


試試這個(gè)方法:


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-26
?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

塊內(nèi)發(fā)現(xiàn)邏輯錯(cuò)誤


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-04-26
  • 3 回答
  • 0 關(guān)注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)