【问题标题】:System.currentTimeMillis() gets disturbed when user manually changes time当用户手动更改时间时 System.currentTimeMillis() 会受到干扰
【发布时间】:2020-04-16 12:54:42
【问题描述】:

我正在构建对时间敏感的应用程序,即某些任务需要在特定时间内完成。有多种获取时间的方法,即System.currentTimeMillis()gpsLocation.getTime()。我不想使用System.currentTimeMillis(),因为用户可以手动更改时间。 gpsLocation.getTime() 呢?我观察到在某些区域,设备无法获取GPS 位置。那我应该使用哪个timestamp

位置时间戳

open fun onLocationChanged(location: Location?) {
    ellapsedTime = location.getTime()
}

我正在检测用户是否使用以下代码更改了自动时间和时区设置:

val isAutoTime = Settings.Global.getInt(activity!!.getContentResolver(), Settings.Global.AUTO_TIME)
        val isAutoTimezone = Settings.Global.getInt(activity!!.getContentResolver(), Settings.Global.AUTO_TIME_ZONE)

我应该使用什么方法来获取时间戳?

问题中缺少什么?

请评论缺少的部分是什么?或者您可能不理解问题/疑虑。我正在写比较时间戳的最佳方法。我添加了使用System.currentTimeinMillis()location.getTime() 的优缺点。由于应用程序依赖于时间,因此如果用户使用时间设置,那么我应该能够比较准确的时间戳而不是系统时间。

【问题讨论】:

  • @OleV.V.:在问题描述I don't want to use System.currentTimeMillis() because user can manually change the time中提到了这一点

标签: android android-location android-gps android-date


【解决方案1】:

从您告诉我们的情况来看,System.nanoTime() 似乎是一个不错的选择。与System.currentTimeMillis() 不同,System.nanoTime() 不受用户手动设置设备系统时钟的影响。因此,即使他们这样做了,在您的程序中,您也可以根据 System.nanoTime() 可靠地计算经过的时间。

System.nanoTme() 承诺只要 JVM 正在运行,就会可靠地测量经过的时间。因此,只要您的程序正在运行,您就有可靠的测量结果。他们说该方法确实为您提供了自设备启动以来的纳秒数,如果是这样,只要设备未重新启动,您还可以跨程序运行进行测量。但不能保证后者,并且可能与下一个 Android 版本有所不同。

有关差异的演示,请参阅此 sn-p:

    long millis0 = System.currentTimeMillis();
    long nanos0 = System.nanoTime();

    System.out.println("Type a line to continue");
    new Scanner(System.in).nextLine();

    long millis1 = System.currentTimeMillis();
    long nanos1 = System.nanoTime();

    System.out.println("Elapsed time according to System.currentTimeMillis(): " + Duration.ofMillis(millis1 - millis0));
    System.out.println("Elapsed time according to System.nanoTime(): " + Duration.ofNanos(nanos1 - nanos0));

我在我的电脑上运行它,并在中间将电脑时钟调快了一个小时。会话看起来像:

Type a line to continue
I have now adjusted the computer clock
Elapsed time according to System.currentTimeMillis(): PT1H16.169S
Elapsed time according to System.nanoTime(): PT28.359865564S

因此,从System.currentTimeMillis() 开始,它错误地看起来已经过了 1 小时 16.169 秒。 System.nanoTime()实话实说:只过去了28.359865564秒。

链接: Documentation of System.nanoTime()

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 2012-01-26
    • 2019-02-09
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多