Android - How to record amount of time that accelerometer is over+below a threshold over a long period
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.
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 o开发者_JS百科f 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? ThanksYou 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:
- On each sensor change event do: prevV = lastV; lastV = movementFlag
- If prevV!=lastV, then we have a change!!
- If LastV==true, it means start of the movement - remember start time (moveStart)
- If it is false, it is the end of the movement - remember end time (moveFinish) and subtract
This way, you may accumulate time on changes only.
精彩评论