Wow, this is definitely a mistake!
The timestamp of each SensorEvent is overwritten as if it were a static variable ...
When I record a timestamp string when an event occurs, all values ββare different.
Events are stored in the array unchanged.
Each SensorEvent element in the array now has the same time stamp, but the arrays of values ββare still different (i.e. they are not the same object and contain different information ... EXCEPT for the time stamp).
Google / HTC, please return 3 hours of my life!
I will write an error report if no one can explain this behavior. This is certainly not described in the API .
In the meantime, try this solution :
import android.hardware.Sensor; import android.hardware.SensorEvent; public class UnbrokenSensorEvent { public long timestamp; public float[] values; public Sensor sensor; public UnbrokenSensorEvent(SensorEvent event){ this.timestamp = event.timestamp; this.values = event.values; this.sensor = event.sensor; } }
Then do something like this in your listener:
ArrayList<UnbrokenSensorEvent> results = new ArrayList<UnbrokenSensorEvent>(); public void onSensorChanged(SensorEvent event) { results.add(new UnbrokenSensorEvent(event)); }
Refactoring should be fairly simple, as SensorEvent and UnbrokenSensorEvent have the same public fields. If you need to use other SensorEvent functions, just run it and paste it into the Unbroken version.
This is a hacked, but IMHO fast hack is always better than waiting for an API update!
source share