Skip to content Skip to sidebar Skip to footer

How To Reset A Step Counter?

I am creating a very minimalistic fitness application for a university assignment that makes use of the Google Maps API and the Android step detector sensors. The issue I cannot r

Solution 1:

You really need to look into SharedPreferences. The value you're getting from sensorEvent.values[0] is immutable by you, so you want to be able to store that value yourself somewhere and SharedPreferences is by far the easiest way that persists over configuration changes.

https://developer.android.com/training/data-storage/shared-preferences

Solution 2:

There is no way to reset sensorEvent.values[0] to 0, There is just one solution for that is to reboot your device which is mentioned in Google Doc. But the best way to do the step counting in your app use the StepDetector sensor. It is just a suggestion :). You can easily reset counting steps using StepDetecor. I am using it in my application and it is giving accurate counting of steps.

in OnCreate Method initialize Sensors like that.

SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if(sensorManager == null){
        stopSelf();
        Toast.makeText(this, "Sensor not found!", Toast.LENGTH_SHORT).show();
    }
    else{
        Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        if(accel!=null){
            sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
        }
        else{
            Toast.makeText(this, "Sorry Sensor is not available!", Toast.LENGTH_LONG).show();
        }
    }

And in the onSensorChanged Method you can use this.

 @Override
publicvoidonSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;

    if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
        numSteps++;
        saveSteps(numSteps);
    }
}

in the "saveSteps" method i am doing:

editor = StepsCountingPrefs.edit();
    editor.putString("WalkingSteps", String.valueOf(numSteps));
    editor.apply();

For Resetting the Steps Code:

editor.putString("WalkingSteps","0");
            editor.apply();
            tv_steps.setText(String.valueOf(0));
            numSteps=0;

Solution 3:

You don't have to use Shared Preferences when you are using STEP_DETECTOR sensor as this sensor resets the number of steps counted to zero when ever it(app) is opened.

Post a Comment for "How To Reset A Step Counter?"