Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Count Steps in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.45k
    Comment on it

    We can count steps in Android by using two sensors i.e. TYPE_STEP_COUNTER and TYPE_STEP_DETECTOR.

    These two sensors were introduced in API level 19.

    TYPE_STEP_COUNTER:- This sensor returns number of steps taken by the user since last reboot. This sensor is implemented in hardware and that's why expected to consume low power. This sensor is ideal for fitness tracking applications.

    TYPE_STEP_DETECTOR:- This sensor triggers an event every time when user takes a step. Each time it returns 1.0. This sensor is only for detecting every individual step as soon as it is taken, for example to perform dead reckoning.

    For adding the above two sensor do the following steps:-

    -> Add the below uses feature in your manifest file

    <uses-feature android:name="android.hardware.sensor.stepcounter" />
    <uses-feature android:name="android.hardware.sensor.stepdetector" />

    -> Declare the following class variables

    private SensorManager mSensorManager;
    private TextView mStepCounterTextView, mStepDetectorTextView; // Textview for showing steps counter of respective sensors
    private boolean isActivityRunning; // if true then activity is in foreground
    private int mStepDetectCounter = 0; // used in step detector sensor

    -> Initialize the text view and mSensorManager in onCreate method. Also check whether your phone supports the required senor or not.

     PackageManager packageManager = getPackageManager();
     if (packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER) && packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_DETECTOR)) {
                mStepCounterTextView = (TextView) findViewById(R.id.step_counter);
                mStepDetectorTextView = (TextView) findViewById(R.id.step_detector);
                mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            } else {
                Toast.makeText(this, "This phone does not supports required sensor", Toast.LENGTH_SHORT).show();
            }
    
    

    -> Implement SensorEventListener and override the required methods onSensorChanged and onAccuracyChanged

    -> Register listener in onResume method

    protected void onResume() {
            super.onResume();
            isActivityRunning = true;
            Sensor countSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
            Sensor detectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
            if (countSensor != null) {
                mSensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
            } else {
                Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show();
            }
            if (detectSensor != null) {
                mSensorManager.registerListener(this, detectSensor, SensorManager.SENSOR_DELAY_UI);
            } else {
                Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show();
            }
        }

    -> Change isActivityRunning value to false in onPause

     protected void onPause() {
            super.onPause();
            isActivityRunning = false;     
        }

    -> Now you can get the step count values in onSensorChanged method. 

    @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            switch (sensorEvent.sensor.getType()) {
                case Sensor.TYPE_STEP_COUNTER:
    
                    if (isActivityRunning) {
                        mStepCounterTextView.setText(String.valueOf(sensorEvent.values[0]));
                    }
                    break;
                case Sensor.TYPE_STEP_DETECTOR:
                    if (isActivityRunning) {
                        mStepDetectCounter++;
                        mStepDetectorTextView.setText(String.valueOf(mStepDetectCounter));
                    }
            }
    
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: