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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…