首先我想每天自動更改textview文本。例如,第一天textview文本是「第一文本」,第二天文本是「第二文本」,在8點鐘eveyday。文本是永久的,直到第二天8點。每天自動更改textview
我嘗試這段代碼,但我的問題是,如果用戶在8點沒有打開此頁面,則不會觸發代碼。文本里面「mquote陣」
SharedPreferences pref =getApplicationContext().getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE) ;
dayCount = pref.getInt("dayCount", 0);
Calendar cal = Calendar.getInstance();
int hour=cal.get(Calendar.HOUR_OF_DAY);
int minute=cal.get(Calendar.MINUTE);
if ((hour==8 && minute==1))
updateQuote();
tvQuote.setText(mquote.getQuote(dayCount));
public void updateQuote()
{
if (dayCount==4)
dayCount=0;
dayCount++;
pref.edit().putInt("dayCount",dayCount).apply();
}
檢查這個https://developer.android.com/training/scheduling/alarms.html – uguboz
而不是檢查準確時間(8:01) ,檢查它是否大於某個值。 –
這不起作用,因爲您正在檢查時間是8:01。你應該檢查時間是否大於8:00,如果你仍然在同一天。 – zed