且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何获得最准确的时间与Android?

更新时间:2023-02-26 18:13:27


  

这可能吗?


块引用>

这取决于你的那个的定义。

那么,让我们先看一下你的材料休息...


  

它将开始与GPS


块引用>

GPS时间不是特别准确的根据硬件。我想你也可能需要使用一个 NmeaListener 来尝试直接解析时间数据出来的NMEA句子,因为据我所知的getTime()位置是系统时间,而不是一些GPS时间。另外,请记住,GPS访问不是通用(用户可能已禁用它专门,用户可能在飞行模式下已经把设备,击>用户可能在一个大的建筑,没有可用的GPS信号)。


  

回落到SNTP


块引用>

有很多SNTP客户端code位在那里,你可以试试。记住,互联网连接是不是万能熊(设备可能是无线网络,只能在外面任何已知的接入点,用户可以在飞行模式下已经把设备,设备可以是移动数据能力,但在当前位置没有信号强度)。


  

如果不工作要求的Andr​​oid通过自己的SNTP或载体NITZ刷新其挂钟与


块引用>

这是可以想象,一个根深蒂固的设备可能能够做到这一点,不知何故,但没有什么在Android SDK对于普通的应用程序,以迫使这些刷新。请记住,连接不通用(见previous款名册)。

所以,要回:


  

这可能吗?


块引用>

如果那个是保证在所有情况下,工作的解决方案,则没有,那是不可能的,因为你不能保证任何与任何时间源的沟通能力。

如果那个是保证设备是否有互联网连接工作的解决方案,那么你需要问你的SNTP客户端库的实现,如果他们能够达到100ms的精度。 SNTP是你的策略的唯一一个完全是你的控制之下,除了连接问题等,如GPS可能不准确和NITZ是不是你自己管理。

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.

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.