I want my application to detect when it is a new day upon startup. If it is a new day I want it to start a new activity and repeat the cycle. for the Next Day.
I start with a calendar instance getting the day of year.
Calendar c = Calendar.getInstance();
int thisDay = c.get(Calendar.DAY_OF_YEAR);
long todayMillis = c.getTimeInMillis();
I then store it in a shared preferences and get another date
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
long last = prefs.getLong("date", System.currentTimeMillis());
c.setTimeInMillis(last);
int lastDay = c.get(Calendar.DAY_OF_YEAR);
I am having trouble running creating the check and looping it everyday.
if (lastDay != thisDay) {
scheduleAlarm();
SharedPreferences.Editor edit = prefs.edit();
edit.putLong("date", todayMillis);
edit.commit();
}
My solution is below.
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();
}