Step counter doesn't reset the step count

爷,独闯天下 提交于 2019-12-01 05:45:34

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!