简体   繁体   English

如果未启用自动时钟,如何知道本地时间与 android 设备之间的时差? 我以编程方式需要它

[英]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.

相关问题 有谁知道如何从Android 2.1设备获取本地日期和时间? - Does anyone know how to get the local date and time from Android 2.1 device? 如何计算UTC时间与当地时间之间的时差 - How to calculate difference between UTC time and local time 如何区分以毫秒为单位的本地时间和以毫秒为单位的unix时间? (从unixtimestamp的转变) - How can I get difference between local time in millisecond and unix time in millisecond? (the shift from unixtimestamp) 如何以毫秒为单位获取 timepicker 选择的时间并比较 android 中两个时间选择器之间的时间差? - How do I get time picked by timepicker in milliseconds and compare time difference between the two time pickers in android? Android如何获取android两个时区的时差? - Android how to get time difference between two time zones in android? 如何检查当前时间和自定义时间是否在30分钟的设备时隙(挂钟时间)内 - How do I check if current time and a custom time are within device time slot (wall clock time) of 30 mins 如何在android模拟时钟上设置时间? - How do I set the time on an android analogue clock? 如何在某个时间以编程方式打开(开机)android设备? - How to programmatically turn on (Power-On) the android device at a certain time? 如何将CET时间转换为设备上的本地时间? - How to convert CET time to local time on device? 如何将时间转换为 UTC,然后转换为设备本地时间 - How to convert a time to UTC and then device local time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM