Android 计步功能-简单实现

时间:2022-05-08 06:45:26

使用Android4.4 Kitkat 新增的STEP DETECTOR 以及 STEP COUNTER传感器。
官方介绍:
TYPE_STEP_COUNTER:计步器(记录历史步数累加值)
int TYPE_STEP_COUNTER


A constant describing a step counter sensor.
A sensor of this type returns the number of steps taken by the user since the last reboot while activated. The value is returned as a float (with the fractional part set to zero) and is reset to zero only on a system reboot. The timestamp of the event is set to the time when the last step for that event was taken. This sensor is implemented in hardware and is expected to be low power. If you want to continuously track the number of steps over a long period of time, do NOT unregister for this sensor, so that it keeps counting steps in the background even when the AP is in suspend mode and report the aggregate count when the AP is awake. Application needs to stay registered for this sensor because step counter does not count steps if it is not activated. This sensor is ideal for fitness tracking applications. It is defined as an REPORTING_MODE_ON_CHANGEsensor.
See SensorEvent.values for more details.
Constant Value: 19 (0x00000013)
TYPE_STEP_DETECTOR:检测器(检测每次步伐数据)

int TYPE_STEP_DETECTOR



A constant describing a step detector sensor.
A sensor of this type triggers an event each time a step is taken by the user. The only allowed value to return is 1.0 and an event is generated for each step. Like with any other event, the timestamp indicates when the event (here the step) occurred, this corresponds to when the foot hit the ground, generating a high variation in acceleration. This sensor is only for detecting every individual step as soon as it is taken, for example to perform dead reckoning. If you only need aggregate number of steps taken over a period of time, register for TYPE_STEP_COUNTER instead. It is defined as a REPORTING_MODE_SPECIAL_TRIGGER sensor.
See SensorEvent.values for more details.
Constant Value: 18 (0x00000012)
使用内容: Sensor: SensorEvent: SensorManager: SensorEventListener:
使用: 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 + "步");
        }
    }
}







Summary
1、计步器数据会在手机重启后清零,因此此处需要注意根据需要来做数据保护。 2、计步器启动需要在检测器启动的基础上才能实现,因此要先启动检测器。 3、

资料: Android4.4 新增传感器 http://blog.objcc.com/android-4-4-kitkat-sensor-batching/
官方文档:https://developer.android.com/reference/android/hardware/Sensor.html#STRING_TYPE_STEP_COUNTER
android中的计步问题及计步传感器分析 http://www.cfanz.cn/index.php?c=article&a=read&id=250334
一个简单的计步器使用Demo: http://blog.csdn.net/aikongmeng/article/details/40457233