计步器不重置计步器 [英] Step counter doesn't reset the step count

查看:107
本文介绍了计步器不重置计步器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过注册和注销侦听器来开始和停止Sensor.TYPE_STEP_COUNTER的录制步骤.

I am able to start and stop recording steps with the Sensor.TYPE_STEP_COUNTER by registering and unregistering the listener.

但是,销毁应用程序时,通过SensorEvent对象传递给我的应用程序的实际值不会重置为零.如果我关闭该应用程序并重新启动它,或者即使我使用更新重新编译了该应用程序,计数器也会从停止的地方开始计数.

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?

推荐答案

来自 Android Sensor API :

公共静态最终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.

(强调我的)

如您所见,在激活传感器的情况下,该值将不断增加,而不会重新设置为零,直到重新启动系统为止.

As you can see, while the sensor is activated, the value will keep increasing without resetting to zero until the system is rebooted.

如果您希望每次启动应用程序时都从0开始计数,则可以将返回的第一个值存储为初始值,然后用它减去后续值.

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.

这篇关于计步器不重置计步器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆