我如何获得最准确的时间与Android? [英] How do I get the most accurate time with Android?

查看:168
本文介绍了我如何获得最准确的时间与Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不认为这个问题是重复的。结果
大多数的回答类似的问题说使用System.currentTimeMillis的()作为最准确的时间,但我已经注意到有两个Android设备并排侧可能会关闭长达5秒钟或更长时间彼此或(更重要的)真正的时间,我相信的currentTimeMillis()将反映这种差异。

所以我真正寻找的是一个COM prehensive解决方案,以获得最大的精确的时间有可能在一个特定的时刻。结果
例如,它将开始与GPS和,如果这是不可用,或者没有信号,回落到SNTP,或者如果不工作要求的Andr​​oid刷新其挂钟通过自己的SNTP或载体NITZ。

我的目标是0.1秒内的精度。结果
这可能吗?结果
谢谢你。


解决方案

  

这可能吗?


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

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


  

它将开始与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.

这篇关于我如何获得最准确的时间与Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆