soren.qvist
soren.qvist

Reputation: 7416

Good strategy for reliable clock in Android?

I'm working on an application that needs to synchronize with a webserver if 24 hours have passed. Every interaction is logged in the database so that I can keep track of the last sync time, so when the application starts, it checks the database to see if it's time for a sync.

So far, I've found no suitable clock for a reliable and consistent time measure, clocks are either temporary (measuring the up-time) or their dates can be changed by the user, so is there any good strategy for dealing with these kind of problems?

Upvotes: 1

Views: 210

Answers (1)

meredrica
meredrica

Reputation: 2563

That's a common problem for everyone in software engineering and the only correct answer is: not without a network call. Measuring time is something you cannot do universally. timezones, different user clocks, daylight savings, all that stuff will happen. Either use System.elapsedRealTime() and something like boot listeners or ask a server what the time is (this is actually a bad idea because it can a) fail b) be terrible slow c) cost the user money and d) wastes battery. Another thing you could do is to automatically sync every day on a specific time (like 4am). This will not help with the user changing the time or DST but it will work in 99% of all scenarios. Users generally do not change their time a lot :)

Upvotes: 2

Related Questions