Answer the question
In order to leave comments, you need to log in
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
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.
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.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question