[英]How to know time difference between the local time and the android device if the auto clock is not enabled ? I need it programmatically
I have a blockchain api with correct time, but when some android devices where the users themselves set their time (auto-clock is not enabled), I am having problem with working with this api because of the difference between these two times.我有一个时间正确的区块链 api,但是当用户自己设置时间的一些 android 设备(未启用自动时钟)时,由于这两个时间之间的差异,我在使用这个 api 时遇到了问题。 Could you help me to identity this difference?
你能帮我找出这个区别吗? System.currrentMillisSecond() method is returning the time set by the user.
System.currrentMillisSecond() 方法返回用户设置的时间。
You don't have other way than syncing with 3rd-party, getting current time from eg own server-side or some web service and counting time basing on it.除了与 3rd 方同步,从例如自己的服务器端或某些 web 服务获取当前时间并根据它计算时间之外,您别无他法。 You may calculate diff between "real-time" and "on-device-time", but still user can minimalize your app and during it's work (
Service
in background?) can change again locally set time您可以计算“实时”和“设备上时间”之间的差异,但用户仍然可以最小化您的应用程序并且在它工作期间(后台
Service
?)可以再次更改本地设置时间
Note that there is also SystemClock.elapsedRealtime()
which returns elapsed time since last boot.请注意,还有
SystemClock.elapsedRealtime()
返回自上次启动以来经过的时间。 This can't be changed by user during runtime (only by restarting device), so may be helpful for counting difference这不能由用户在运行时更改(只能通过重新启动设备),因此可能有助于计算差异
val synced = getTimestampFromWeb() // instead of unreliable System.currrentMillisSecond()
val elapsedWhenSynced = SystemClock.elapsedRealtime()
// store these in some
...
// use below line anytime later
val currentTime = synced + (SystemClock.elapsedRealtime() - elapsedWhenSynced)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.