我希望我的应用程序检测启动时的新的一天.如果是新的一天,我希望它开始新的活动并重复循环.为第二天.
我从日历实例开始,获取一年中的某一天.
Calendar c = Calendar.getInstance();
int thisDay = c.get(Calendar.DAY_OF_YEAR);
long todayMillis = c.getTimeInMillis();
然后我将其存储在共享首选项中并获取另一个日期
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
long last = prefs.getLong("date", System.currentTimeMillis());
c.setTimeInMillis(last);
int lastDay = c.get(Calendar.DAY_OF_YEAR);
我无法运行创建支票并每天循环检查.
if (lastDay != thisDay) {
scheduleAlarm();
SharedPreferences.Editor edit = prefs.edit();
edit.putLong("date", todayMillis);
edit.commit();
}
我的解决方案如下.
Calendar c = Calendar.getInstance();
int thisDay = c.get(Calendar.DAY_OF_YEAR);
long todayMillis = c.getTimeInMillis();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
long last = prefs.getLong("date", System.currentTimeMillis());
c.setTimeInMillis(last);
int lastDay = c.get(Calendar.DAY_OF_YEAR);
// Toast.makeText(getApplicationContext(),
// "lastday " + lastDay + "thisDay " + thisDay, Toast.LENGTH_LONG)
// .show();
if (lastDay == thisDay) {
scheduleAlarm();
SharedPreferences.Editor edit = prefs.edit();
edit.putLong("date", todayMillis + 86400000);
edit.commit();
}