android 计步功能原理,Android开发——计步功能
一、使用Android內(nèi)置傳感器?TYPE_STEP_COUNTER?和?TYPE_STEP_DETECTOR
TYPE_STEP_COUNTER 記錄開機以來的總步數(shù),適合用于開發(fā)計步器。
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; // 自應(yīng)用運行以來STEP_DETECTOR檢測到的步數(shù)
private int mStepCounter = 0; // 自系統(tǒng)開機以來STEP_COUNTER檢測到的步數(shù)
@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("設(shè)備檢測到您當前走了%d步,自開機以來總數(shù)為%d步", mStepDetector, mStepCounter);
mStepTV.setText(desc);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
}
計步效果:當用戶走了一定的步數(shù)后,兩個計步器的步數(shù)才會開始變化。區(qū)別在于每次行走時,TYPE_STEP_COUNTER 會緩存步數(shù),當檢測到步數(shù)大于10步時,TYPE_STEP_COUNTER 會一次性加10步,后續(xù)的步數(shù)就實時變化,直到用戶停止行走。而TYPE_STEP_DETECTOR 不會緩存步數(shù),只有用戶走了8~10步時,TYPE_STEP_DETECTOR 才會開始連續(xù)記錄步數(shù),直到用戶停止行走。
總結(jié)
以上是生活随笔為你收集整理的android 计步功能原理,Android开发——计步功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python sqlalchemy or
- 下一篇: vue基础(学习官方文档)