A
A
Artem2012-11-21 16:04:20
Android
Artem, 2012-11-21 16:04:20

How to know the time difference between device and server?

There is a server and an Android device. The server tells the device the end time of the event in Unix Time, and the device displays a countdown in seconds (for example, "1:32"). The problem is that the time on the device can be very slow or fast plus network delays. Accordingly, it is necessary to synchronize somehow. The question is how?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin Kitmanov, 2012-11-21
@bartwell

Also, keep in mind that the user may have a crookedly set time zone, and his GMT0 may differ from the true one :)
In general, in order to solve this, the server must send not only the end time of the event, but also “what time is it server on GMT0". Further it is necessary to receive time, specific to the client, on GMT0. We subtract one from the other and immediately get a correction for both network lags and the wrong clock in the device.

N
nicolausYes, 2012-11-21
@nicolausYes

There is an NTP time synchronization protocol for networks with variable delays, you need to build on it and implement it in a similar way. NTP uses the Marzullo algorithm, which takes into account the transfer time.
On stackoverflow , a person described in detail how the protocol works, gave an example, I think this is what you need.
Also on the open spaces of the network there are pieces of code that implement NTP.
There is an open source application for android to synchronize the phone with an NTP server.

P
Pavel Isakov, 2012-11-21
@kwikpik

Send to the device not the end time of the event, but how many ms are left before it ends. In this case, the error will be only at the time of sending and receiving data.

J
jov, 2012-11-21
@jov

In my application (monitoring), I started a separate thread, which, once an hour, synchronizes the internal clock of the application with the server, via ntp. I do not trust the time set by the user on the system absolutely.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question