单击按钮后 x 秒内获取传感器数据

发布于 2024-12-21 07:52:58 字数 1662 浏览 3 评论 0原文

我仍在学习 Android 开发,如果这个问题看起来很愚蠢,我很抱歉,但我试图仅在有限的时间内从手机获取加速度计数据。目标是在单击按钮后仅记录约 3 秒的传感器 x 坐标值。

public class AccelerometerTestingActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;
    float x_event[];
    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        // add listener. The listener will be HelloAndroid (this) class
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_GAME);

    }

    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }

    public void onSensorChanged(SensorEvent event){

        // check sensor type
        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

            // assign directions
            float x=event.values[0];
            x_event=addArrayElement(x_event,x);
        }
    }

    private float[] addArrayElement(float[] currentArray, float x) {
        // TODO Auto-generated method stub
        float newArray[] = new float[currentArray.length+1];
        int i= 0;

        for(i=0;i<currentArray.length;i++)
        {
            newArray[i] = currentArray[i];
        }

        newArray[i+1]=x;

        return newArray;
    }

所以 x_event 是一个数组,每个 SENSOR_DELAY_GAME 定期填充加速度计的 X 位置。

问题是,此代码记录了活动运行时的所有值。

我想要的是这个数组仅从单击按钮时开始填充,并且仅填充 x 秒(尚未决定 x 的值)。之后,该数组将被传递给另一个函数进行分析。

我只是不明白如何及时限制监听器。

感谢您的帮助。

I'm still learning Android Development so sorry if this question seems stupid but I'm trying to obtain accelerometer data from the Phone for a limited time only. The goal is for the x-coordinate values from the sensor to be recorded only for about 3 seconds after a button is clicked.

public class AccelerometerTestingActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager;
    float x_event[];
    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        // add listener. The listener will be HelloAndroid (this) class
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_GAME);

    }

    public void onAccuracyChanged(Sensor sensor,int accuracy){

    }

    public void onSensorChanged(SensorEvent event){

        // check sensor type
        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

            // assign directions
            float x=event.values[0];
            x_event=addArrayElement(x_event,x);
        }
    }

    private float[] addArrayElement(float[] currentArray, float x) {
        // TODO Auto-generated method stub
        float newArray[] = new float[currentArray.length+1];
        int i= 0;

        for(i=0;i<currentArray.length;i++)
        {
            newArray[i] = currentArray[i];
        }

        newArray[i+1]=x;

        return newArray;
    }

So x_event is an array filled with the X position of the Accelerometer periodically every SENSOR_DELAY_GAME.

The thing is, this code records all values while activity is running.

What I would like is for this array to be filled only from the time a button is clicked, and filled only for x seconds (haven't decided on the value of x yet). After that the array would be passed to another function for analysis.

I just don't understand how to limit the Listener in time.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-12-28 07:52:58

这是一个快速解决方案;
在注册监听器后,在 onCreate() 中,您可以执行以下操作:

    final SensorEventListener listener = this;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            sensorManager.unregisterListener(listener);
        }
    }, theTimeInMilliseconds);

您还应该考虑使用列表而不是数组来保存值,这样您就不必每次都重新创建它获得新值的时间:

ArrayList<Float> xEvent = new ArrayList<Float>();

并添加项目:

xEvent.add(event.values[0]);

Here's a quick solution;
in your onCreate() after you have registered your listener, you can do this:

    final SensorEventListener listener = this;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            sensorManager.unregisterListener(listener);
        }
    }, theTimeInMilliseconds);

You should also consider using a list rather than an array to save your values, so you don't have to recreate it every time you get a new value:

ArrayList<Float> xEvent = new ArrayList<Float>();

And to add items:

xEvent.add(event.values[0]);
懵少女 2024-12-28 07:52:58

您是否考虑过使用一个计时器,在 3 秒延迟后,
- 取消注册监听器
- 调用您的分析功能

Have you considered using a timer that would, after a 3 secs delay,
- unregister the listener
- call your analysis function
?

水波映月 2024-12-28 07:52:58

解决了问题

只需使用 Timer 类方法每 1 秒执行一次

new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                                     // Just calculate your accleration here

                              }
                                                 }, 0, 1000);

我认为更好的解决方案

Solved the Problem

Just use Timer class method do this every 1s

new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                                     // Just calculate your accleration here

                              }
                                                 }, 0, 1000);

A better Solution I think

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文