一个按钮的onClick后获得的x秒传感器数据 [英] Obtaining sensor data for x seconds after onClick of a button

查看:212
本文介绍了一个按钮的onClick后获得的x秒传感器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习Android的发展很抱歉,如果这个问题似乎愚蠢的,但我想从手机获得加速度的数据只在有限的时间。要单击一个按钮后约3秒才会记录是针对来自传感器的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;
    }

所以x_event是填充有加速度计的每SENSOR_DELAY_GAME X位置周期性阵列。

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

问题是,当活动正在运行此code记录所有的值。

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

我想是只从按钮被点击的时间来填补这个数组,并填充只为x秒(还没有决定x的值还)。在这以后,阵列将被传递到用于分析另一功能

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.

感谢您的帮助。

推荐答案

下面是一个快速的解决方案;
在你的的onCreate()您已经注册了监听器后,你可以这样做:

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>();

和添加项目:

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

这篇关于一个按钮的onClick后获得的x秒传感器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆