0

I'm trying to do a step counter program that records your steps and multiplies by 0.04 to get your calories.

The problem is that the step counter keeps refreshing without clicking on the reset button. It goes to 12 steps, then goes to 0.

I have no clue what to try. I am new in android and I don't really know how to mess with sensors.

package igor.finaltp;


import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    TextView tv_steps;
    TextView tv_calories;
    Sensor mStepDetectorSensor;
    SensorManager sensorManager;
    boolean running = false;
    Button btn_reset;
    int pace = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_steps = (TextView) findViewById(R.id.tv_steps);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mStepDetectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        sensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_NORMAL);
        btn_reset = (Button) findViewById(R.id.btn_reset);
        btn_reset.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                    pace = 0;
                    tv_steps.setText("0");
                    tv_calories.setText("0");
                    return true;
            }
        });
        tv_calories = (TextView) findViewById(R.id.tv_calories);

    }

    @Override
    protected void onResume(){
        super.onResume();
        //ao entrar na app ele escuta o sensor, passando a activity, o sensor e o tempo de intervalo
        sensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }



    @Override
    protected void onStop() {
        super.onStop();
        //ao sair do app ele para de escutar o sensor passando a actovity e o sensor
        sensorManager.unregisterListener(this, mStepDetectorSensor);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor sensor = sensorEvent.sensor;

        if(sensor.getType() == Sensor.TYPE_STEP_DETECTOR){
            pace++;
            tv_steps.setText("" + pace);

            double calories = pace*0.04;
            tv_calories.setText(String.format("%.2f", (calories)) + " Kcal lost");
        }
    }

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

    }
}

I expect somewhere around 10 steps when I walk for 10-15 steps I get the right amount of steps but it refreshes somehow to 0 steps

Fei
  • 1,906
  • 20
  • 36
Magorda
  • 1
  • 1

1 Answers1

0

Activity will restart upon orientation change. Your pase variable will also reset to 0. You could lock the orientation or save instance state for your application upon orientation change.

See this:

Fei
  • 1,906
  • 20
  • 36
  • You might trigger the activity restart in other conditions. Consider putting your state into a persistent storage. See this: https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/data/data-storage.html – Fei Jun 08 '19 at 13:43