好主意!
在我的第一个活动我设置这样的事情里面的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