Android Sensor Timestamp 参考时间

发布于 2024-12-09 23:18:06 字数 329 浏览 1 评论 0原文

我正在从 SensorEvent 数据中读取时间戳值,但无法计算出这些值的参考时间。 Android 文档只是说“事件发生的时间(以纳秒为单位)” 举个例子:

我当前的 Android 设备日期,2011 年 10 月 14 日 23:29:56.421 (GMT+2)

System.currentTimeMillis * 1000000 (nanosec) = 1318627796431000000 (即好的)

sensorevent.timestamp(纳秒)= 67578436328000 = 19 小时 46 分钟 ????

你可以帮我吗?

谢谢

I'm reading timestamp values from SensorEvent data but I can't work out the reference time for these values. Android documentation just says "The time in nanosecond at which the event happened" As an example:

My current Android device date, October 14th 2011 23:29:56.421 (GMT+2)

System.currentTimeMillis * 1000000 (nanosec) = 1318627796431000000 (that's ok)

sensorevent.timestamp (nanosec) = 67578436328000 = 19 hours 46 min ????

May you help me?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-12-16 23:18:06

看来您正在处理的是自操作系统启动以来的纳秒数,也称为“正常运行时间”。

有关此问题的更多信息:http://code.google.com/ p/android/issues/detail?id=7981

我应该补充一下链接的问题SensorEvent.timestamp 到绝对 (utc) 时间戳? 处理相同的问题,并且在哪里我找到了答案。

It appears that what you are dealing with is the number of nanoseconds since the operating system started, also known as "uptime".

Further info on the issue: http://code.google.com/p/android/issues/detail?id=7981

I should add that the linked question SensorEvent.timestamp to absolute (utc) timestamp? deals with the same issue and is where I found the answer.

暮年 2024-12-16 23:18:06

我知道这是一个非常古老的问题,但是,我也在努力将 SensorEvent.timestamp 转换为人类可读的时间。因此,我在这里写下我到目前为止所理解的内容以及我如何将其转换,以便从你们那里获得更好的解决方案。任何意见都将受到欢迎。

据我了解,SensorEvent.timestamp是自设备启动以来经过的时间。所以我必须知道设备的正常运行时间。所以如果有一个返回设备启动的API,那就很容易了,但是,我还没有找到。
因此,我使用 SystemClock.elapsedRealtime() 和 System.currentTimeMillis() 来“估计”设备的正常运行时间。这是我的代码。

private long mUptimeMillis; // member variable of the activity or service
...
atComponentsStartUp...() {
    ...
    /* Call elapsedRealtime() and currentTimeMillis() in a row 
       in order to minimize the time gap */
    long elapsedRealtime = SystemClock.elapsedRealtime();
    long currentTimeMillis = System.currentTimeMillis();

    /* Get an uptime. It assume that elapsedRealtime() and
       currentTimeMillis() are called at the exact same time. 
       Actually they don't, but, ignore the gap 
       because it is not a significant value.
       (On my device, it's less than 1 ms)   */
    mUptimeMillis = (currentTimeMillis - elapsedRealtime); 
    ....
}
...
public void onSensorChanged(SensorEvent event) {
    ...
    eventTimeMillis = ((event.timestamp / 1000000) + mUptimeMillis);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(eventTimeMillis);
    ...
}

我认为这适用于毫秒时间错误是可以的应用程序。请留下你的想法。

I know that it's a very old question, but, I'm also struggling for converting SensorEvent.timestamp to a human readable time. So I'm writing here what I've understood so far and how I'm converting it in order to get better solutions from you guys. Any comments will be welcomed.

As I understood, SensorEvent.timestamp is an elapsed time since the device's boot-up. So I have to know the uptime of the device. So if there is an API returning device's boot-up, it will be very easy, but, I haven't found it.
So I'm using SystemClock.elapsedRealtime() and System.currentTimeMillis() to 'estimate' a device's uptime. This is my code.

private long mUptimeMillis; // member variable of the activity or service
...
atComponentsStartUp...() {
    ...
    /* Call elapsedRealtime() and currentTimeMillis() in a row 
       in order to minimize the time gap */
    long elapsedRealtime = SystemClock.elapsedRealtime();
    long currentTimeMillis = System.currentTimeMillis();

    /* Get an uptime. It assume that elapsedRealtime() and
       currentTimeMillis() are called at the exact same time. 
       Actually they don't, but, ignore the gap 
       because it is not a significant value.
       (On my device, it's less than 1 ms)   */
    mUptimeMillis = (currentTimeMillis - elapsedRealtime); 
    ....
}
...
public void onSensorChanged(SensorEvent event) {
    ...
    eventTimeMillis = ((event.timestamp / 1000000) + mUptimeMillis);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(eventTimeMillis);
    ...
}

I think this works for Apps that a millisecond time error is okey. Please, leave your ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文