码迷,mamicode.com
首页 > 移动开发 > 详细

Step Detector and Step Counter Sensors on Android

时间:2014-08-18 10:27:43      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:android   style   http   os   io   strong   for   ar   

Step Detector and Step Counter Sensors on Android

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); 

bubuko.com,布布扣

No special permissions are required.

Step Detector and Step Counter Sensors on Android,布布扣,bubuko.com

Step Detector and Step Counter Sensors on Android

标签:android   style   http   os   io   strong   for   ar   

原文地址:http://www.cnblogs.com/wanqieddy/p/3918818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!