I am able to start and stop recording steps with the Sensor.TYPE_STEP_COUNTER
by registering and unregistering the listener.
However, the actual value that is passed to my app via the SensorEvent
object does not reset to zero when the application is destroyed. If I close the app and restart it, or even if I recompile my app with updates, the counter picks up where it left off.
If I run other apps that use the step counter sensor, they independently count their steps and reset their counter.
Does the sensor have a cache that is app specific? What is the correct way to reset the sensor to zero steps counted?
From Android Sensor API:
public static final int TYPE_STEP_COUNTER
A sensor of this type returns the number of steps taken by the user since the last reboot while activated. The value is returned as a float (with the fractional part set to zero) and is reset to zero only on a system reboot.
(Emphasis mine)
As you can see, while the sensor is activated, the value will keep increasing without resetting to zero until the system is rebooted.
If you want to count from 0 every time the app is started, you can store the first value returned as initial value, then subtract subsequent value by it.
The Sensor's onSensorChanged method is only called when a real new value appears, it doesn't trigger randomly with repeated values, so if you want to count the steps yourself, you can.
I've chosen to place the steps value inside SharedPreferences, and whenever the onSensorChanged method is triggered I increment the value I have by 1, it's reliable because the sensorChanged method does not trigger for repeated values, this solution works well for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With