0

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;
        }

}

1 Answer 1

0

you added the permission in the manifest file ?

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
2
  • Yes, I did. Turns out my phone doesn't have the STEP_COUNTER, so I had to use the accelerometer.
    – Martina
    Commented Feb 14, 2023 at 8:55
  • can you share the processing you did to detect steps from the accelerometer axes signal please ! Commented Feb 14, 2023 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.