Martina
Martina

Reputation: 1

Step Counter in Android Studio doesn't count steps

I have a problem with an app I'm trying to develop in Android Studio. I'm trying to develop a step counter using the STEP_COUNTER sensor in a Fragment.I decided to show my results in a TextView ('mStepsSinceReboot'), but it keeps being empty. Do you have any idea on how to make it work?

An hypotesis is that I should use a Service, but online I only found implementations that don't require it.

Thanks in advance for your answer. This is the code of my Fragment.

public class StepFragment extends Fragment implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mStepCounter;
    private boolean isSensorPresent;
    private TextView mStepsSinceReboot;

    public StepFragment() {
        // Required empty public constructor
    }

    public static StepFragment newInstance(String param1, String param2) {
        StepFragment fragment = new StepFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.fragment_step, container, false);

        mStepsSinceReboot=v.findViewById(R.id.stepssincereboot);

        mSensorManager=(SensorManager) this.getActivity().getSystemService(getContext().SENSOR_SERVICE);
        if (mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
            mStepCounter=mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);

            isSensorPresent=true;
        }
        else{
            isSensorPresent=false;
        }
        return v;
    }

        @Override
        public void onSensorChanged(SensorEvent event) {
            mStepsSinceReboot.setText(("Steps since reboot: "+String.valueOf(event.values[0])));
            Log.d("Passi", String.valueOf(mStepsSinceReboot));
        }

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

    @Override
    public void onResume(){
        super.onResume();
        if(isSensorPresent){
            mSensorManager.registerListener(this,mStepCounter,SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    public void onPause(){
        super.onPause();
        if(isSensorPresent){
            mSensorManager.unregisterListener(this);
        }
    }


    @Override
        public void onDestroy(){
            super.onDestroy();

            mSensorManager=null;
            mStepCounter=null;
        }

}

Upvotes: 0

Views: 797

Answers (1)

EL Amine Bechorfa
EL Amine Bechorfa

Reputation: 251

you added the permission in the manifest file ?

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

Upvotes: 0

Related Questions