Android - 如何记录加速度计在很长一段时间内超过或低于阈值的时间量
在我的应用程序中,我想计算加速度计超过特定阈值的时间,但我不确定如何执行此操作。
我的加速度计的灵敏度设置为 'SensorManager.SENSOR_DELAY_GAME'
,因此 onSensorChanged()
方法每秒被调用大约 20 次。
我有一个名为 movementFlag
的标志,每当加速度计速度超过阈值时,我将其设置为 true,并在低于阈值时设置回 false。
所以说在一小时的时间内,我想计算 movementFlag
设置为 true 的总时间,每次设置时都会增加这个时间值,并且设置时也是如此为假。
我想也许我可以有 2 个值 - moveStart
和 moveFinish
- 我可以在 moveFlag 从真与假,反之亦然。将这些值相减就可以得到每次设置标志的时间。
但我不知道如何在标志转换时执行此操作,而不是在设置标志时始终执行此操作。
谁能告诉我如何执行此操作或其他方法来完成我想做的事情?谢谢
In my app I want to count for how long the accelerometer is over a certain threshold, but i'm unsure of how to do this.
The sensitivity of my accelerometer is set to 'SensorManager.SENSOR_DELAY_GAME'
so the onSensorChanged()
method is called approximately 20 times a second.
I have a flag called movementFlag
that I'm setting to true whenever the accelerometer speed goes over a threshold, and setting back to false when it goes under the threshold.
So say over the space of an hour, I want to count up the total amount of time that the movementFlag
is set to true, incrementing this time value each time it's set, and the same for when it's set to false.
I thought maybe I could have 2 values - moveStart
and moveFinish
- that I could set to System.currentTimeMillis()
upon the transition of movementFlag from true to false and vice versa. Subtracting these values from each other would give me the amount of time the flag is set each time.
But I don't know how to do this just on the transition of a flag and not the whole time while a flag is set.
Can anyone tell me how to do this or another method to do what I want to do here? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要存储两个值才能知道转换何时发生,一个值存储 movingFlag 的最后一个值(我们称之为lastV),另一个值存储之前的值(prevV)。然后:
这样,您可以仅在更改时累积时间。
You need to store two values to know when transition occurs, one which stores last value of your movementFlag (let's call it lastV), and one which stores value before that one (prevV). Then:
This way, you may accumulate time on changes only.