Time for action – using the step counter sensor in activity
In this section, we will learn how to use the step counter sensor with a simple example. The good thing about the step counter is that, unlike other sensors, your app doesn't need to tell the sensor when to start counting the steps and when to stop counting them. It automatically starts counting as soon as the phone is powered on. To use it, we just have to register the listener with the sensor manager and then unregister it after using it. In the following example, we will be showing the total number of steps taken by the user since the last reboot (power on) of the phone in the android activity:
- We create a
StepsCounterActivity
, which implements theSensorEventListener
interface so that it can receive the sensor events. We initiate theSensorManager
andSensor
objects of the step counter, and we also check the sensor availability in theOnCreate()
method of the activity. We register the listener in theonResume()
method and unregistered...