简体   繁体   English

Android加速度计每次都返回相同的值

[英]Android Accelerometer return same value every time

I'm trying to record 4 sample per second value of accelerometer.So I repeat my logic every 250 millisecond(I have also tried as 4 sampple per second). 我试图记录每秒4个采样的加速度计值,所以我每250毫秒重复一次逻辑(我也尝试过每秒4个采样)。 Every time ends of my method I unregister sensor and register again when my method start(when I'm not unregister the sensor my logic not works in onSensorChanged and it continuously records all data). 每次方法结束时,我都会注销传感器并在方法启动时再次注册(当我不注销传感器时,我的逻辑在onSensorChanged中不起作用,并且会连续记录所有数据)。

By this approach(register and unregister sensor every second or every quarter second) I'm able to mange the sample rate of accelerometer but every time I have return same value. 通过这种方法(每秒钟或每四分之一秒对传感器进行一次注册和注销),我能够管理加速度计的采样率,但是每次返回相同的值时,我都可以进行管理。

Here is my onSensorChanged method. 这是我的onSensorChanged方法。

   @Override
    public void onSensorChanged(final SensorEvent event) {

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                            sensorManager.registerListener(JumpingBall.this, accelerometer, sensorManager.SENSOR_DELAY_NORMAL);

                float deltaX1 = (float) ((event.values[0])/9.81);
                float deltaY1 = (float) ((event.values[1])/9.81);
                float deltaZ1 = (float) ((event.values[2])/9.81);

                int i=0;
                for (i=0;i<1;i++) {

                    listx.add(String.valueOf(deltaX1));
                    listy.add(String.valueOf(deltaY1));
                    listz.add(String.valueOf(deltaZ1));
                }
                sensorManager.unregisterListener(JumpingBall.this);

   }


        }, 0, 250);//put here time 1000 milliseconds=1 second\
    }

If any other info needed please inform me. 如果需要其他信息,请通知我。

Don't unregister and register the listener every time. 不要每次都注销并注册侦听器。 Try to register the listener in your onCreate or onStart methods, and unregister the listener on your onFinish. 尝试在onCreate或onStart方法中注册侦听器,然后在onFinish上注销侦听器。

I am pretty sure your errors are coming from the fact that you are seeming to "reset" your accelerometer every time you detect a change. 我很确定您的错误是由于您似乎每次检测到更改都会“重置”加速度计而产生的。

In other words, remove your calls 换句话说,删除通话

sensorManager.registerListener(JumpingBall.this, accelerometer, sensorManager.SENSOR_DELAY_NORMAL);

and

sensorManager.unregisterListener(JumpingBall.this);

from your onSensorChanged method and put your sensorManager.registerListener in your main/when you start your context and put your sensorManager.unregisterListener in your onFinished/when you are done with your context. onSensorChanged方法开始,并在启动上下文时将sensorManager.registerListener放在主/中,在完成上下文时将sensorManager.unregisterListener放在sensorManager.unregisterListener中。

https://developer.android.com/guide/topics/sensors/sensors_motion.html https://developer.android.com/guide/topics/sensors/sensors_motion.html

Here is also a link to some examples of how to set up certain sensors, including the accelerometer. 这也是一些如何设置某些传感器(包括加速度计)的示例的链接。 You can see how they initialize the sensors all at the top, and then use onSensorChanged to record the data. 您可以在顶部看到它们如何初始化传感器,然后使用onSensorChanged记录数据。

EDIT: 编辑:

Below is what I would do. 下面是我会做的。 Make your onSensorChanged still records the event every time the accelerometer senses something, which is what you want to do. 使您的onSensorChanged每次加速度计感测到某件事时仍会记录该事件,这就是您要执行的操作。 MAKE SURE YOU REGISTER YOUR LISTENER IN YOUR onCreate. 确保在onCreate.注册您的onCreate. This is very important. 这个非常重要。 Then, when your timer ticks, make sure that you record the values. 然后,当计时器计时时,请确保您记录了这些值。 Make sure that your variables are available for your onSensorChanged and timer.scheduleAtFixedRate methods by making them package-private (ie make sure you initialize them at the top of your class). 通过将变量设置为包私有,确保变量可用于onSensorChangedtimer.scheduleAtFixedRate方法(即,确保将其初始化为类的顶部)。 You also don't need a for loop that only runs once, then its not a loop. 您也不需要只运行一次的for循环,然后就不需要循环了。

SO, the idea is read the values always, but only record them every 250ms. 因此,我们的想法是始终读取值,但每250ms才记录一次。 As another user pointed out you are also making a new timer every time a new event happens which is not what you want to do. 正如另一位用户指出的那样,每次发生新事件时,您也要创建一个新计时器,这不是您想要的。 Try implementing the code below: 尝试实现以下代码:

//Initialize these up top
private float deltaX1 = 0;
private float deltaY1 = 0;
private float deltaZ1 = 0; 

@Override
public void onSensorChanged(final SensorEvent event) {

            deltaX1 = (float) ((event.values[0])/9.81);
            deltaY1 = (float) ((event.values[1])/9.81);
            deltaZ1 = (float) ((event.values[2])/9.81);

}


timer.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {

        listx.add(String.valueOf(deltaX1));
        listy.add(String.valueOf(deltaY1));
        listz.add(String.valueOf(deltaZ1));

    }, 0, 250);//put here time 1000 milliseconds=1 second\
}

You might have to add the timer code to your onCreate or onStart methods, I am not sure what your project looks like, but implement it where it is appropriate. 您可能必须将计时器代码添加到onCreateonStart方法中,我不确定您的项目是什么样,但是可以在适当的地方实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM