好主意!
在我的第一個活動我設置這樣的事情裏面的onCreate:
Calendar c = Calendar.getInstance();
int currentTimeSeconds = c.get(Calendar.SECOND);
SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edittime = share.edit();
edittime.putInt("timevalue",currentTimeSeconds);
edittime.commit();
,並使用你的代碼裏面的onCreate另一項活動:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int secondsPreviousDay = prefs.getInt("seconds", 0);
if (secondsPreviousDay != 0){ //means there was an earlier set value from previously entering the activity
//compare if more than 3600*24 = 86400 (1 day in seconds) had passed
if (currentTimeSeconds - secondsPreviousDay > 86400){
// mymethod();
mymethod();
}
}
else {
prefs.edit().putInt("seconds", currentTimeSeconds).apply();
}
}
,我感到抱歉,但我還在學習
新的一天在對比的是什麼?如果相隔一天,您需要參考另一個日期進行比較。 – chRyNaN
這可能很難 – Kamil
完全沒有。將值存儲在SharedPreferences中。然後在需要時檢索它,並且有條件確定它是否在一天之後。 – chRyNaN