Search code examples
javaandroidtimeaccelerometer

Most reliable way of calculating time difference


I have accelerometer readings that are updated from the onSensorChanged() function each time the accelerometer values change. I then create a file where I write the accelerometer readings (X,Y,Z) axis on each line but would also like to add the time intervals between each reading. I was reading somewhere that using System.currentTimeMillis() isn't the way to go. What would be the best way of doing this as accurately as possible?


Solution

  • I suggest to use the timestamp field of a SensorEvent:

    public long timestamp
    
    Since: API Level 3
    The time in nanosecond at which the event happened
    

    Just remember the last value and calculate the difference that way