How to get the most accurate time with Android?

I really don't think this question is a duplicate.
Most of the answers to such questions say that using System.currentTimeMillis () as the most accurate time, but I noticed that two Android devices can be disconnected side by side for up to 5 seconds or more from each other or (more importantly) in real time, and I believe that currentTimeMillis () will reflect this difference.

What I'm really looking for is a comprehensive solution that allows you to get the most accurate time at the moment.
For example, it will start with GPS and if it is not available or there is no signal, it will return to SNTP, or if it does not work, ask Android to update its wall clock using its SNTP or NITZ carrier.

My goal is accuracy within .1 seconds.
Is it possible?
Thanks.

+6
source share
1 answer

Is it possible?

It depends on your definition of "one."

So first, consider the rest of your material ...

it will start with GPS

GPS time is not particularly accurate depending on the hardware . I think you will also need to use NmeaListener to try to parse the time data from the NMEA sentences directly, since AFAIK getTime() on Location is the system time, and not some GPS time. In addition, keep in mind that access to GPS is not universal (the user can turn it off specifically, the user could place the device on an airplane, the user can be in a large building without available GPS signals).

back to SNTP

There are many bits of SNTP client code that you can try. Keep in mind that the Internet connection is not universal (the device can only have Wi-Fi outside any known access point, the user could put the device in flight mode, the device can be mobile, but not have signal strength at the current location).

if this does not work, ask Android to update its wall clock using its own SNTP or NITZ NICZ operator

It can be assumed that the root device could have done it somehow, but there is nothing in the Android SDK for regular applications to force such an update. Keep in mind that the connection is not universal (see the list in the previous paragraph).

So back to:

Is it possible?

If β€œthis” is a solution that is guaranteed to work in all cases, then no, this is not possible, since you are not guaranteed any opportunity to communicate with any source of time.

If β€œthis” is a solution that is guaranteed to work if the device has an Internet connection, then you will need to ask your SNTP client library library performers if they can achieve 100 ms accuracy. SNTP is the only one of your strategies that is completely under your control, except for the connection problem, since GPS can be inaccurate, and NITZ is not something you manage yourself.

+6
source

All Articles