Android 4.4 – KitKat – Step detector code

One of the new features in Android 4.4, KitKat is the “Step Counter” and “Step Detector” sensors.

I wrote a quick sample app that utilizes the step counter. It acts like a simple pedometer, registering for the sensor events when it starts, and updating the UI with the latest step count whenever a new event is returned.

This updating won’t be in real-time, as the system batches up events. The code currently specifies SensorManager.SENSOR_DELAY_NORMAL as the update frequency – this amounts to 200000 microseconds, or 5 updates a second (should be good enough, even if you duct tape your phone to a cheetah).

I’m using the TYPE_STEP_COUNTER Sensor, which dispatches an event with the total number of steps periodically when a new step is detected. The other Sensor is TYPE_STEP_DETECTOR, which fires a timestamp sensitive event with every detected step (rather than the total number of steps detected so far).

For now, this only works on a Nexus 5 running KitKat, since the API was added in KitKat and the Nexus 5 is the first device to ship with a dedicated low-power step detecting chip.

The code is available on GitHub: android-step-counter

Image

Advertisements

7 thoughts on “Android 4.4 – KitKat – Step detector code

  1. Thanks for this, I was looking for a quick sample to confirm that I was doing things the right way.

    One interesting thing that I ran into is that the step counter only counts (at all) when there is a listener registered on it. So you basically have to use a service and receive every step, even if you only want to display it on the screen when the user opens your app. You can’t just re-register and say “how many steps have we walked now?” because it won’t have counted any.

    1. Yeah, I noticed that too – that’s part of why I’m never unregistering the listener from the service (so it keeps counting in the background).

      I would guess it’s designed like that so the sensor isn’t activated if nobody cares about the events in order to save power?

  2. Yeah, I would imagine that is the reason.

    I think I’m going to write it as a bindable service instead, so the value ticks along in the background and then the UI Activity can grab the details if it is running.

  3. Thanks for this!

    I’m trying to write an app to count every day’s steps. So I need to start a service everyday at 12:00 pm to get the step counter’s reading, in order to calculate the difference between two days’ readings. But unfortunately my service got killed quite often by the system so the service can not be up and running everyday at midnight, thus the step counting is not always correct. Is there a better way to get one days accurate steps in a way other than mine please?

    Thanks!

    1. Unless things have changed in the last couple of years, the step counter will not count at all unless a listener is registered to it, so you have to run a full-time service to make sure it keeps counting. If you want to stop it being killed you’ll have to run it as a foreground service with a notification icon. I can’t think of any other way around this. Note also that running the step counter full time may affect the device’s battery life, though it shouldn’t be significant for devices with a dedicated hardware sensor.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s