美文网首页
Android 计步功能-简单实现

Android 计步功能-简单实现

作者: V_boomboom | 来源:发表于2016-07-22 21:43 被阅读2247次

使用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

另一种方法实现计步数据
http://blog.csdn.net/finnfu/article/details/45273183

相关文章

  • Android 计步功能-简单实现

    使用Android4.4 Kitkat 新增的STEP DETECTOR 以及 STEP COUNTER传感器。 ...

  • iOS 计步器附demo

    简介: 很多app都实现了计步这个功能,现在的人们对健康关注度越来越高,所以说来简单写了一个计步功能的demo 实...

  • android计步功能初探

    在市面上浏览过众多的计步软件,可惜没有开源的代码,而github上的几个开源的计步代码,要么就是记得不准,要么就是...

  • 手机计步功能开发所遇到的问题

    最近做健康相关的项目,需要实现计步和睡眠的统计。本文主要记录计步功能实现过程和问题以及部分问题解决方案。计步处理主...

  • Android计步(简单Demo)

    18.03.01 附上Demo链接,欢迎探讨撕咬~ Pedometer 18.02.23 最近接了个测试各大手机厂...

  • Android计步统计实现方案

    计步服务使用这个库 https://github.com/jiahongfei/TodayStepCounter ...

  • GitHub标星5.3K,字节跳动大神教你Android 实现登

    Android 实现登录界面和功能实例 最近一个android小程序需要登录功能,我简单实现了一下。现在记录下来也...

  • MVP 笔记

    MVP 与 MVC 简单介绍 实践 参考资料:Android MVP 详解(下)一步一步实现 Android 的M...

  • Android NFC功能 简单实现

    昨天公司突然有个要用到NFC 功能的项目,我一想前几年挺火的,网上肯定也有不少例子,觉得分分钟搞定的,然而…… 虽...

  • Android简单实现录音功能

    最近项目APP遇到需要添加录音功能,由于我是Java,半路出道做Android,所以第一想到的就是百度... 然而...

网友评论

      本文标题:Android 计步功能-简单实现

      本文链接:https://www.haomeiwen.com/subject/zowijttx.html