1

I have some things in my Android application that need to update once per day.

It's pretty simple I think, but I have no idea in what format I need to format the date and time (if time is needed) and how to check if an update has been done today (where today is between 00:01am and 23:59pm in the user's local time). The update should not be done if it was already done for today.

Here's what I DO know how to do:

  • Save the last update date in SharedPreferences (but how do I get a string of it, I do not know)
  • Get things from SharedPreferences (but I don't know how to compare dates in string format)
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118

5 Answers5

9

It is irrelevant what format you choose. It is just matter of recalculations.

I'd suggest using milliseconds since epoch, as all system calls use it, so it would be easier for you to use the same.

As 1000 millis is 1 second it's easy to figure out that 1000*60*60*24 equals to one day (24hrs). So, if storedMillis is bigger than NOW - 1000*60*60*24, (and NOW is i.e. System.currentTimeMillis()), then it is too early to do the check. If storedMillis is smaller, then save your NOW timestamp and do the check:

long now = System.currentTimeMillis();
long diffMillis = now - lastCheckedMillis;
if( diffMillis >= (3600000  * 24) ) {
  // store now (i.e. in shared prefs)

  // do the check
} else {
   // too early
}

EDIT

I am interested in doing it when the app is first opened for the current day, even if the last update was done 10 minutes ago.

It's just the matter how to get the proper millis to compare against. Replace long now = System.currentTimeMillis(); from above code with following code block:

Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);

long now = cal.getTimeInMillis();

which shall do the trick.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Although this is a great answer, I think that you got my question wrong. I am not interested in doing an update if the last update was more than 24 hours from now. I am interested in doing it when the app is first opened for the current day, even if the last update was done 10 minutes ago. Imagine this: last update is done January 4th at 23:55. I open the app again Jan 5, 00:05. Using your code the update will not run when it should. I will have to wait for another 23+ hours and reopen the app for the update to be activated, resulting in a day without an update. – Dzhuneyt Nov 22 '12 at 16:14
2

If you store your date in format 20121122 (YYYYmmdd) then you can compare is like 20121122 > 20121123. But it must be stored as int or cast to int when comparing.

Martin Milesich
  • 184
  • 3
  • 8
1

Store the timestamp (System.currentTimeMillis() ) of the Last execution and compair it with the currient one. If the difference is more than 24 hours... You know it or?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • See my comment on @WebnetMobile.com's answer. This approach is not suitable, because then I can have days without an update if the app is updated at around 23:59 the previous day. – Dzhuneyt Nov 22 '12 at 16:17
1

Set up an Alarm with AlarmManager that executes every 24 hours, then do stuff

Check this question: Alarm Manager Example

It's a more complicated approach than the rest, but makes sure things are done, while with the other options the app must be executed in order to check if it has to update whatever.

Community
  • 1
  • 1
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • I have commented on @WebnetMobile.com's answer as to why this approach is not suitable in my case. [Link to comment.](http://stackoverflow.com/questions/13514907/update-only-once-per-day/13515014#comment18502830_13515090) – Dzhuneyt Nov 22 '12 at 16:16
  • Then accept his answer and search for a Preferences example, the job is done in this thread :) – Maragues Nov 22 '12 at 16:54
1

Here is the method

public boolean runOnceADay() {
    SharedPreferences shp= c.getSharedPreferences(Constants.GENERAL_SHP, MODE_PRIVATE);
    SharedPreferences.Editor editor= shp.edit();
    long lastCheckedMillis = shp.getLong(Constants.ONCE_A_DAY, 0); // "KEY" you may change yhe value
    long now = System.currentTimeMillis();
    long diffMillis = now - lastCheckedMillis;
    if (diffMillis >= (3600000 * 24)) { // set up your time circulation
         editor.putLong(Constants.ONCE_A_DAY, now);
        editor.commit();
        Util.showMessage(c, "Once a Day Test");
        return false;
     } else {
        Util.showMessage(c, "Too Early");
        return true;
    }
}
Samir
  • 6,405
  • 5
  • 39
  • 42