android 计步功能原理,Android开发——计步功能

一、使用Android内置传感器 TYPE_STEP_COUNTER 和 TYPE_STEP_DETECTOR

TYPE_STEP_COUNTER 记录开机以来的总步数,适合用于开发计步器。

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.

TYPE_STEP_DETECTOR 检测到用户走了一步就向SensorEventListener传递一个浮点值1.0

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.

布局只是一个TextView,代码就略过了,下面是主程序:

package com.lee.sensordemo;

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.widget.TextView;

public class TestStepActivity extends Activity {

private TextView mStepTV;

private SensorManager mSensorManager;

private MySensorEventListener mListener;

private int mStepDetector = 0; // 自应用运行以来STEP_DETECTOR检测到的步数

private int mStepCounter = 0; // 自系统开机以来STEP_COUNTER检测到的步数

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test_step);

mStepTV = findViewById(R.id.step_tv);

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

mListener = new MySensorEventListener();

}

@Override

protected void onResume() {

super.onResume();

mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),

SensorManager.SENSOR_DELAY_NORMAL);

mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),

SensorManager.SENSOR_DELAY_NORMAL);

}

@Override

protected void onPause() {

super.onPause();

mSensorManager.unregisterListener(mListener);

}

class MySensorEventListener implements SensorEventListener {

@Override

public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {

if (event.values[0] == 1.0f) {

mStepDetector++;

}

} else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {

mStepCounter = (int) event.values[0];

}

String desc = String.format("设备检测到您当前走了%d步,自开机以来总数为%d步", mStepDetector, mStepCounter);

mStepTV.setText(desc);

}

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}

}

908724e1e4cbb502ffe1e3a9bc397a5b.png

计步效果:当用户走了一定的步数后,两个计步器的步数才会开始变化。区别在于每次行走时,TYPE_STEP_COUNTER 会缓存步数,当检测到步数大于10步时,TYPE_STEP_COUNTER 会一次性加10步,后续的步数就实时变化,直到用户停止行走。而TYPE_STEP_DETECTOR 不会缓存步数,只有用户走了8~10步时,TYPE_STEP_DETECTOR 才会开始连续记录步数,直到用户停止行走。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值