One of the new features in Android 4.4, KitKat is the “Step Counter” and “Step Detector” sensors.
Step 2: Get SensorManager and Step Sensors
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
Step 3: Implement SensorEventListener.onSensorChanged method
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
textView.setText("Step Counter Detected : " + value);
} else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// For test only. Only allowed value is 1.0 i.e. for step taken
textView.setText("Step Detector Detected : " + value);
}
}
Step 4: Register and Unregister Sensors
Step 5: Update Android Manifest configuration for these new features
res/values/strings.xml:
<string name="step_counter">
android.hardware.sensor.stepcounter
</string>
<string name="step_detector">
android.hardware.sensor.stepdetector
</string>
AndroidManifest.xml
<uses-feature
android:name="@string/step_detector" android:required="false"/>
<uses-feature
android:name="@string/step_counter" android:required="false"/>
Step 6: Test run your simple pedometer application
I hope this example will be good enough and helpful in getting started with new Step related features on Android 4.4.
Step 1: Create SensorEventListener
public class MainActivity extends Activity implements SensorEventListener{
private TextView textView;
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
Step 2: Get SensorManager and Step Sensors
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
textView.setText("Step Counter Detected : " + value);
} else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// For test only. Only allowed value is 1.0 i.e. for step taken
textView.setText("Step Detector Detected : " + value);
}
}
Step 4: Register and Unregister Sensors
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,
mSensorManager.registerListener(this, mStepCounterSensor,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,
mSensorManager.registerListener(this, mStepDetectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
Step 5: Update Android Manifest configuration for these new features
res/values/strings.xml:
<string name="step_counter">
android.hardware.sensor.stepcounter
</string>
<string name="step_detector">
android.hardware.sensor.stepdetector
</string>
AndroidManifest.xml
<uses-feature
android:name="@string/step_detector" android:required="false"/>
<uses-feature
android:name="@string/step_counter" android:required="false"/>
Step 6: Test run your simple pedometer application
Step 7: Your Homework : Extend and customize your application by managing step sensor data as per your ideas
No comments:
Post a Comment