Let’s see how we can interact with these sensors. Basically, there are 2 sensors.
- Step Counter: This keeps a count of the number of steps that you have taken. The counter is only reset when you re-boot the device, else, for every step you take (or the phone thinks you took, you counts up).
- Step Detector: This sensor just detects when you take a step. That’s it.
// Step Counter
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float steps = event.values[0];
textViewStepCounter.setText((int) steps + “”);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
SensorManager.SENSOR_DELAY_UI);
// Step Detector
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// Time is in nanoseconds, convert to millis
timestamp = event.timestamp / 1000000;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
SensorManager.SENSOR_DELAY_UI);
No special permissions are required.
Head over to Github to get the full source code.
Hello,
Is there any way to detect the end of walking? I want to do something when user stops after walking.
Not one I know about. I doubt there will be such a feature.
Hi i tried your code and it gives me a time stamp 1 jan 1970 and also no steps are been accounted for?
Ok. Let me check. Will post an update here.