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.