[Q] Stop accelerometer listener after fixed amount of time

I like to start a listener; e.g. accelerometer sensor and have it stop after like 20 seconds or so. How can I force it to stop after criteria is reached (e.g 20 seconds is passed)

Here is the base code of what I have:

public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;

@
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
//...
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

}

@
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}

}

private void getAccelerometer(SensorEvent event) {
float[] values = event.values.clone();

float x = values[0];
float y = values[1];
float z = values[2];

//...
}

@
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@
protected void onResume() {
super.onResume();
// register this class as a listener for the accelerometer sensor
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELER OMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}

@
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}

All help is appreciated.

Thanks

Show Accepted Answer

Guest Quick Reply (No URL, BBcode or HTML)

Last post by cruecker
3 hours ago
Last post by ehackett
1 hour ago
Last post by yvonne01
16 minutes ago
Last post by ivah.mcglynn
2 hours ago
Last post by veronica.jacobi
4 hours ago