Search code examples
javaandroidandroid-sensorssensormanager

My step counter does not reset the steps, even after uninstall


I use Sensor.TYPE_STEP_COUNTER I know it only resets when rebooting. Is there an alternative way to reset the steps to 0 when pressing a button?

Please see my code, you will find the Runactivity.class

Maybe I can do it in another way which resets the steps. without having me to reboot every time.

public class RunActivity extends AppCompatActivity implements SensorEventListener{

    private SensorManager sensorManager;
    private TextView count;
    boolean activityRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Bundle bundle = getIntent().getExtras();
        final String naam   = bundle.getString("naam");
        TextView NaamView = null;

        Button stopRun = (Button) findViewById(R.id.stopRun);

        count = (TextView) findViewById(R.id.countView);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        NaamView =  (TextView) findViewById(R.id.naamRunText);

        NaamView.setText(naam);

        stopRun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String countValue = count.getText().toString();



                Log.d("countVAL", String.valueOf(countValue));


                Intent myIntent = new Intent(RunActivity.this, HomeScreenActivity.class);

                Bundle bundle = new Bundle();


                bundle.putString("naam", naam);
                sensorManager.flush(RunActivity.this);
                sensorManager.unregisterListener(RunActivity.this);
                count.setText("0");
                onStop();

                myIntent.putExtras(bundle);

                startActivity(myIntent);


            }
        });

    }



    @Override
    protected void onResume() {
        super.onResume();
        activityRunning = true;
        Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if(countSensor != null){
            sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);

        }else{
            Toast.makeText(this, "Jouw apparaat heeft geen sensor!", Toast.LENGTH_LONG) .show();
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if(activityRunning){
            count.setText(String.valueOf(event.values[0]));
        }else{
            event.values[0] = 0;
        }



    }



    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
        activityRunning = false;
    }



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

    }

    protected void onDestroy() {
        super.onDestroy();

    }
}

Solution

  • I searched on it and tried to do it with different ways. Nothing Helped. Then I found the most simple way possible.

    In onSensorChanged() just add the counter so when ever onSensorChanged() will be called (it will be called on every step), counter will simply count the steps then show this counter to your UI instead of showing the value of event.values[0]

    on your Reset button make the counter 0 again.