开发者

Android - How to record amount of time that accelerometer is over+below a threshold over a long period

开发者 https://www.devze.com 2023-02-13 23:35 出处:网络
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.

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 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? Thanks


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:

  1. On each sensor change event do: prevV = lastV; lastV = movementFlag
  2. If prevV!=lastV, then we have a change!!
  3. If LastV==true, it means start of the movement - remember start time (moveStart)
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号