Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
275 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...