Step Counter App With Android KitKat 4.4

Today I will write a simple app for you that utilizes the step counter. This is a sample app same like the pedometer app which will register the sensor events when it starts, and update the UI with the latest step count whenever a new event is detected by the app.

The code will specify SensorManager.SENSOR_DELAY_NORMAL as the update frequency.

I will be using TYPE_STEP_COUNTER Sensor, which dispatches an event with the total number of steps periodically when a new step is detected.

Make sure that You must check the code on a Nexus 5 running KitKat, because this API was added in KitKat and the Nexus 5 is the first device to ship with a dedicated low-power step detecting chip.

Now follow the steps below to implement the step detecting app.

Step1) Create a new androject in your android IDE.

Step2)  Write following code into main layout file:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Step count since reboot:"/>
<TextView android:id="@+id/count"
android:textSize="36dp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

 

 

Step3) Write following code into your main java file

 

package com.example.stepcounter;

import android.app.Activity;
import android.content.Context;
import android.hardware.*;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

private SensorManager sensorManager;
private TextView count;
boolean activityRunning;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count = (TextView) findViewById(R.id.count);

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}

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

}

@Override
protected void onPause() {
super.onPause();
activityRunning = false;
// if you unregister the last listener, the hardware will stop detecting step events
// sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0]));
}

}

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

 

 

Note

Run this app on a device supporting android 4.4 or higher version and you will see the counter increasing with every step you reach.

step1

Leave a Comment: