4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SensorEventのtimestampについてのメモ

Posted at

SensorEvent.timestampはシステムブート時からの経過時間(ナノ秒)。
じゃあSensorEventをもらった時のいわゆるUNIX時間をもらいたいときはどうしたらいいんだ?となって書いてみたのが以下のコード。

MySensorListener.java

long eventOccuredTimeMilli = -1;
long eventOccuredTimeNano = -1;

@Override
public void onSensorChanged(SensorEvent event){
   if (eventOccuredTimeMilli < 0){
      eventOccuredTimeMilli = System.currentTimeMillis();
      eventOccuredTimeNano = event.timestamp;
   }
   long unixtime = eventOccuredTimeMilli
             + (event.timestamp - eventOccuredTimeNano) / 1000000;
   // 後略
}

イケてない。。。

SensorEvent.timestampの値ってSystem.nanoTime()でもらえる値と一緒なのでしょうか。C++力低すぎて追いかけきれませんでした。リスナー作る時に一回調べるだけで済ませたいですよね。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?