Reputation: 2497
I really don't think this question is a duplicate.
Most answers to similar questions say to use System.currentTimeMillis() as the most accurate time, but I've noticed that two Android devices side-by-side may be off up to 5 seconds or more from each other or (more importantly) the real time, and I believe currentTimeMillis() will reflect that difference.
So what I'm really looking for is a comprehensive solution to get the most accurate time possible in a given moment.
For example, it would start with the GPS and, if that's not available or there's no signal, fall back to SNTP, or if that doesn't work ask Android to refresh its wall clock with via its own SNTP or carrier NITZ.
My goal is accuracy within .1 seconds.
Is that possible?
Thanks.
Upvotes: 9
Views: 5647
Reputation: 6733
Simple SNTP Client class for retrieving network time on Android (SNTPClient)
Get the SNTP Helper class from the below repository
https://github.com/aslamanver/sntp-client-android
Copy the SNTPClient.java
into your project, and there you go. It's ready.
Usage Retrieve the time of a specific time zone.
SNTPClient.getDate(TimeZone.getTimeZone("Asia/Colombo"), new SNTPClient.Listener() {
@Override
public void onTimeResponse(String rawDate, Date date, Exception ex) {
}
});
Current time zone can be passed to retrieve the current time of the device zone.
Calendar.getInstance().getTimeZone();
About more
The output time is formatted according to ISO 8601 format.
yyyy-MM-dd'T'HH:mm:ssZ
Time server host is Google
time.google.com
Upvotes: 1
Reputation: 1007286
Is that possible?
That depends upon your definition of "that".
So, let's examine the rest of your material first...
it would start with the GPS
GPS times are not especially accurate depending on hardware. I think you'd also need to use an NmeaListener
to try to parse the time data out of the NMEA sentences directly, as AFAIK getTime()
on Location
is the system time, not some GPS time. Also, bear in mind that GPS access is not universal (user may have disabled it specifically, user might have put device in airplane mode, user may be in a large building with no available GPS signals).
fall back to SNTP
There are plenty of SNTP client code bits out there that you can try. Bear in mind that Internet connectivity is not universal (device may be WiFi-only outside of any known access point, user might have put device in airplane mode, device may be mobile-data-capable but have no signal strength in current location).
if that doesn't work ask Android to refresh its wall clock with via its own SNTP or carrier NITZ
It's conceivable that a rooted device might be able to do this, somehow, but there's nothing in the Android SDK for ordinary apps to force such a refresh. Bear in mind that connectivity is not universal (see roster in previous paragraph).
So, going back to:
Is that possible?
If "that" is a solution that is guaranteed to work in all cases, then no, it is not possible, as you are not guaranteed any ability to communicate with any time source.
If "that" is a solution that is guaranteed to work if the device has Internet connectivity, then you'd need to ask your SNTP client library implementers if they can achieve 100ms accuracy. SNTP is the only one of your strategies that is completely under your control, other than the connectivity issue, as GPS may not be accurate and NITZ isn't something you manage yourself.
Upvotes: 8