How to create walking step counter App using Accelerometer sensor and Shared Preference in Android?

This video shows the steps to use the accelerometer sensor in your App. The use case taken in this video is about counting steps using the accelerometer sensor data. It retrieves the acceleration along x,y and z axis. To compute the step movement, the algorithm used in the App is very simple. It simply takes the magnitude of the acceleration along the 3 axis. Then it calculates the difference of the acceleration magnitude from the previous values. If this value is greater than a certain threshold (For walking greater than 6 and for running greater than 10) then it does the counting accordingly. In this video the climbing of stairs is not considered as separate acceleration along the y-axis data is not considered to avoid unnecessary complicating the algorithm and making the video lengthy.

Towards the later part of the video it is shown in the App that how to use the SharedPReferences to store the data for subsequent running of the App. The data is saved in the Shared Preference of the App during onPause and onStop method. Using onResume method, the data is retrieved back for the subsequent use.

In the end it is shown that how can you quickly create a APK file for your App which can be used to install the App on your Phone.

Steps:

  • Get Accelerometer data:
    • Acceleration along X-Axis
    • Acceleration along Y-Axis
    • Acceleration along Z-Axis
XYZ_Axis
  • Get the magnitude of the 3-D acceleration data

Sqrt (x_accelration^2 + y_accelration^2 + z_accelration^2)

  • Get the difference in this magnitude from the previous value
  • If this value is greater than a particular threshold value then increment the steps count.
    • Threshold for walking = 6
    • Threshold for running = 10

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

package com.example.mycustomstepcounter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private double MagnitudePrevious = 0;
private Integer stepCount = 0;

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

textView = findViewById(R.id.textView);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

SensorEventListener stepDetector = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent!= null){
float x_acceleration = sensorEvent.values[0];
float y_acceleration = sensorEvent.values[1];
float z_acceleration = sensorEvent.values[2];

double Magnitude = Math.sqrt(x_acceleration*x_acceleration + y_acceleration*y_acceleration + z_acceleration*z_acceleration);
double MagnitudeDelta = Magnitude – MagnitudePrevious;
MagnitudePrevious = Magnitude;

if (MagnitudeDelta > 6){
stepCount++;
}
textView.setText(stepCount.toString());
}
}

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

sensorManager.registerListener(stepDetector, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

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

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.putInt(“stepCount”, stepCount);
editor.apply();
}

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

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.putInt(“stepCount”, stepCount);
editor.apply();
}

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

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
stepCount = sharedPreferences.getInt(“stepCount”, 0);
}
}

10 comments

  1. I tried doing it this way and it kinda works but it adds the steps only if the phone is going like crazy fast like when I run or just throw around the phone. How do I make it measure movement when I’m walking at a normal speed?

  2. I think to increase the sensitivity, you have to change the below line of code:

    if (MagnitudeDelta > 6){

    In this if you use smaller number say 4 or 3 then it will become more sensitive and most likely for normal walk also it will capture.

    As I have stated in my video also, capturing the accurate results is based on a advanced algorithm. There are ample of these algorithms available on google. Due to time constraint, I couldn’t show any of the complex algorithm implementation in the App within video.

    Cheers
    Programmer World

    1. I think the complete Java code has been shared on this page. Can you please help me to know which specific code section I should share? I will be happy to share further details.

      Cheers
      Programmer World

      1. I think the MainActivity code is shared on this page. For the XML code of the layout, it is shown in the video and will automatically get created when the layout is designed for the App in the Android Studio. If anything more is required then please elaborate. We will be glad to mention that.

        Cheers
        Programmer World

  3. how stop pause, and resume are working even they have not called.
    pls guide me for stop, pause and resume the counter.

      1. The count value across pause, stop and resume is managed by using SharedPreferences variable. This variables gets saved in the App data itself. So, in the subsequent run of the App the previous value of the SharedPreferences variable is fetched. This concept is used in most of the counting APPs. Look for below lines in the code above:

        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.putInt(“stepCount”, stepCount);
        editor.apply();

        Also, please note, if one clears the App’s data or reinstalls the App then most likely this SharedPreferences variable will also get deleted (unless App data is preserved during the reinstallation). So, one should be careful with this limitation while using SharedPreferences. As a workaround, one should transfer the value of this variable to any server database to avoid the loss of the information.

        I hope the above explanation helps.

        Cheers
        Programmer World

    1. onPause, onResume, etc. are system methods and even if they are not explicitly called, they work/ are called when the App is paused (sent to background), stopped (the App process is killed) and resumed (re-instated as the active App).

      I hope the above explanation helps.

      Cheers
      Programmer World

Leave a Reply

%d bloggers like this: