Step Detector and Step Counter Sensors on Android

时间2014-03-31 11:56:00 Tech Droid 原文 http://techdroid.kbeanie.com/2014/03/step-detector-and-step-counter-sensors.html

Android KitKat has added a few more hardware sensors to it's API list. Step Sensors are one of them, which looks very promising. Although, not a lot of phones yet have these Step Sensors, in the future, this would gradually become a standard I think. Currently, Nexus 5 has them.

Let's see how we can interact with these sensors. Basically, there are 2 sensors.

  1. Step Counter: This keeps a count of the number of steps that you have taken. The counter is only reset when you re-boot the device, else, for every step you take (or the phone thinks you took, you counts up).
  2. Step Detector: This sensor just detects when you take a step. That's it.

The example project shows you how to initialize and setup the SensorManager and respond to events from the Sensors.

// Step Counter
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float steps = event.values[0];
textViewStepCounter.setText((int) steps + "");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
SensorManager.SENSOR_DELAY_UI);

// Step Detector
sManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// Time is in nanoseconds, convert to millis
timestamp = event.timestamp / 1000000;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
SensorManager.SENSOR_DELAY_UI);

No special permissions are required.

更多相关文章

  1. Android--Alarm,定时闹钟
  2. Android(安卓)Init Language(android init.rc语法)
  3. Android(六): What Android(安卓)Is
  4. Fragments: The Solution to All of Android's Problems
  5. 最易用的 Android(安卓)HTTP library
  6. android:repeatCount="infinite"在set中无效
  7. android:repeatCount="infinite"在set中无效
  8. Android(安卓)TimePicker
  9. Android智能推荐、MVP架构电商应用、markdown编辑器、多种动画效

随机推荐

  1. Ubunu下搭建android NDK环境
  2. Android保存32位BMP格式图片
  3. android web services6
  4. ActionBar神奇魅力2
  5. 自定义控件attrs文件里的属性
  6. Android并发修改异常:java.util.Concurren
  7. 【Android 应用开发】Android 开发环境下
  8. android listview实现表格样式
  9. Android(安卓)TextView内部padding问题
  10. Android SDK版本更新