如何获得在原生的Andr​​oid code当前时间? [英] How to get the current time in native Android code?

查看:166
本文介绍了如何获得在原生的Andr​​oid code当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有一个简单的方法来获得原生的Andr​​oid code当前时间。最理想这将是System.getTimeMillies相媲美的东西()。我只会用它来看到某些函数调用需要多长时间这么长的变量,以毫秒为单位的当前时间将是我的最佳解决方案。

I was wondering if there is an easy way to get the current time in native Android code. Optimally it would be something comparable to System.getTimeMillies(). I will only be using it to see how long certain function calls will take so a long variable with the current time in milliseconds would be the optimal solution for me.

在此先感谢!

推荐答案

有关微秒分辨率,你可以使用函数gettimeofday()。这将使用挂钟时间,即继续推进时,该设备是睡着了,但如有突然变化向前或向后如果网络更新设备的时钟。

For microsecond resolution you can use gettimeofday(). This uses "wall clock time", which continues to advance when the device is asleep, but is subject to sudden shifts forward or backward if the network updates the device's clock.

您也可以使用clock_gettime(CLOCK_MONOTONIC)。本品采用单调时钟,从未跨越式前进或后退,不过,当设备休眠停止计数。

You can also use clock_gettime(CLOCK_MONOTONIC). This uses the monotonic clock, which never leaps forward or backward, but stops counting when the device sleeps.

定时器的实际分辨率是依赖于设备的。

The actual resolution of the timers is device-dependent.

这两者都是POSIX API的,不Android的特定

Both of these are POSIX APIs, not Android-specific.

这篇关于如何获得在原生的Andr​​oid code当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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