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

安卓 计步传感器

时间:2017-10-29 16:00:46      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:时报   port   tty   void   code   nexus   public   stand   def   

概念及原理

计步传感器介绍

    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.

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

Step Detector: This sensor just detects when you take a step. That’s it.@link

Step counter sensor

    TYPE_STEP_COUNTER:计步器(记录历史步数累加值)

    这种类型的传感器返回用户自上次重新激活以来所采取的步骤数。 该值作为浮点数返回(小数部分设置为零),仅在系统重新引导时才将其重置为零。 事件的时间戳设置为采取该事件的最后一步的时间。 该传感器以硬件实现,预计功耗低。 如果要持续跟踪长时间的步数,请勿取消注册该传感器,以便即使AP处于挂起模式,也会在后台继续计数步骤,并且当AP处于挂起状态时报告聚合计数 苏醒。 应用程序需要保留该传感器的注册,因为如果没有激活步进计数器不计数步骤。 该传感器适用于健身跟踪应用。 它被定义为REPORTING_MODE_ON_CHANGE传感器。

Step detector sensor

    TYPE_STEP_DETECTOR:检测器(检测每次步伐数据)

    这种类型的传感器每次用户触发一个事件。 唯一允许的返回值为1.0,并为每个步骤生成一个事件。 与任何其他事件一样,时间戳表示事件(这里是步骤)何时发生,这对应于当脚撞到地面时,产生加速度的高变化。 该传感器仅用于检测每个单独的步骤,例如执行航位推算。 如果您只需要在一段时间内累积的步数,请注册TYPE_STEP_COUNTER。 它被定义为REPORTING_MODE_SPECIAL_TRIGGER传感器。@link

 

 

API部分

使用:@link
1、使用传感器之前首先获取SensorManager通过系统服务获取:
SensorManager  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
2、获取我们需要的传感器类型:
//单次有效计步
Sensor  mStepCount = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
//系统计步累加值
Sensor  mStepDetector = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
3、注册监听者(监听传感器事件)
mSensorManager.registerListener(this, mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
PS:取消注册:
mSensorManager.unregisterListener(this, mStepDetector);
mSensorManager.unregisterListener(this, mStepCount);
4、实现SensorEventListener接口,重写方法并获取数据:
从监听到的传感器事件中选取合适的类型,获得数据:
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == sensorTypeC) {
            //event.values[0]为计步历史累加值
        tvAllCount.setText(event.values[0] + "步");
    }
    if (event.sensor.getType() == sensorTypeD) {
        if (event.values[0] == 1.0) {
            mDetector++;
            //event.values[0]一次有效计步数据
            tvTempCount.setText(mDetector + "步");
        }
    }
}

参考:

    Android计步模块优化 项目应用

    android-4-4-step-detector-and-counter

     Android 计步功能-简单实现

     android计步功能初探 基于加速度传感器

安卓 计步传感器

标签:时报   port   tty   void   code   nexus   public   stand   def   

原文地址:http://www.cnblogs.com/zhen-android/p/7750151.html

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