I am using an Accelerometer in my application and I am wondering if I can make it the way that accelerometer is only activates for a certain amount of time after the user has pressed a start button. It should stops when user press the stop button. It will essentially work as a pedometer which is part of a bigger application. This is the code used to implement the accelerometer:-
SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
int sensorType = Sensor.TYPE_ACCELEROMETER;
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
I have the code counting the steps which is:-
final SensorEventListener sensorListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
if(event.values[2]<3)
{
counter ++;
}
else if(event.values[2]>5)
{
counter --;
counter ++;
}
sensorData.setText("Steps = "+ counter);
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
};
I'm just wondering that since I'm counting the steps in the onSensorChanged method how do I limit this to only a button press state? Would it be possible to add a timer in to count how long has passed between the two button presses I mentioned earlier?