Android Question Date and Time...AARRGGHH!!

karld

Active Member
Licensed User
Longtime User
And things were coding so well...

I have got my app pulling an update file from Google Firebase. That part is working great.

What is stumping me now is how to get the app to only check once every 30 days for an update.
I think it is silly to have it go out and check every time the app starts.

I have been reading up on dateutils and frankly, my head is spinning.

Anyone have an easy way to just look at todays date, compare it to the last date the update was done and fire it off if 30 days or more have gone by. Then save current date and repeat?

This one is driving me crazy..
 

canalrun

Well-Known Member
Licensed User
Longtime User
Yes. I don't have code handy, but what you want to do is convert the date values to the Integer format (milliseconds since January 1, 1970, I think).

Take the date value of the last update, convert to the Integer format, add the equivalent of 30 days of milliseconds (30 * 24 * 60 * 60 * 1000 (There is probably a date utilities function that does this)). Call this the next update check date.

Compare today's date in this Integer format against the next update check date. If today's date is greater then check for updates.

Barry.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
And things were coding so well...

What is stumping me now is how to get the app to only check once every 30 days for an update.


try this .. Probably the flaw here is if the app is not started for a long period (weeks /months between starts)
But this should get you going ...
B4X:
Sub CheckUpdate

    DateTime.DateFormat= "dd-MM-yyyy"
    Dim today As Long = DateTime.Now

    If Not(File.Exists(File.DirInternal,"Update.txt")) Then   
        'on very first run / install write install date to file
        File.WriteString(File.DirInternal, "Update.txt", DateTime.Date(today))   
    Else
        Dim lastDate As Long = DateTime.DateParse(File.ReadString(File.DirInternal,"Update.txt"))
        Dim p As Period
        p = DateUtils.PeriodBetweenInDays(lastDate,today)
        Log(p)

        If p.Days > 29 Then
          'Do your Update    
          File.WriteString(File.DirInternal, "Update.txt", DateTime.Date(today)) 'write new date to file
        End If
    End If

End Sub
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The simplest way would be to save the date (in which format you fancy the most) when the update is done, and on app start check it against current date
 
Upvote 0

karld

Active Member
Licensed User
Longtime User
try this .. Probably the flaw here is if the app is not started for a long period (weeks /months between starts)
But this should get you going ...
B4X:
Sub CheckUpdate

    DateTime.DateFormat= "dd-MM-yyyy"
    Dim today As Long = DateTime.Now

    If Not(File.Exists(File.DirInternal,"Update.txt")) Then  
        'on very first run / install write install date to file
        File.WriteString(File.DirInternal, "Update.txt", DateTime.Date(today))  
    Else
        Dim lastDate As Long = DateTime.DateParse(File.ReadString(File.DirInternal,"Update.txt"))
        Dim p As Period
        p = DateUtils.PeriodBetweenInDays(lastDate,today)
        Log(p)

        If p.Days > 29 Then
          'Do your Update   
          File.WriteString(File.DirInternal, "Update.txt", DateTime.Date(today)) 'write new date to file
        End If
    End If

End Sub
This works like a charm! Thanks...
Been testing for the last few days and it is perfect.
Thank You!
 
Upvote 0
Top