你所需要的是这样的:
一个SharedPreference
与描述的最后日期很值。
一个Calendar
。
当你的活动开始时(在onCreate()
方法似乎是一个很好的地方),你可以得到一个新的Calendar
。这将有当前的日期,你可以提取当天:
Calendar c = Calendar.getInstance();
int thisDay = c.get(Calendar.DAY_OF_YEAR); //You can chose something else to compare too, such as DATE..
long todayMillis = c.getTimeInMillis(); //We might need this a bit later.
然后,您可以读取最后一次可能保存的值和日历设置为这个值,那么今天的比较:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long last = prefs.getLong("date", 0); //If we don't have a saved value, use 0.
c.setTimeInMillis(last);
int lastDay = c.get(Calendar.DAY_OF_YEAR);
if(last==0 || lastDay != thisDay){
//New day, update TextView and preference:
SharedPreferences.Editor edit = prefs.edit();
edit.putLong("date", todayMillis);
edit.commit();
}
我不明白问题 – njzk2 2012-04-12 15:01:03
我正在创建一个基本报价应用程序,并且希望包含当天页面的报价。为了做到这一点,我需要知道在订单日期是否已经改变了应用程序更改报价,我希望这是有道理的 – user983965 2012-04-12 15:03:35
是的,它的作用。你大概可以存储在SharedPreferences一天(DAY_OF_YEAR在日历)和一年,活动期间比较值启动 – njzk2 2012-04-12 15:06:42