Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

android - Obtaining sensor data for x seconds after onClick of a button

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...