Obtaining sensor data for x seconds after onClick

2019-08-17 05:39发布

问题:

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.

回答1:

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


回答2:

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



回答3:

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