原文网址:http://blog.csdn.net/aikongmeng/article/details/40457233
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 官方参考
下载
先简单实现下功能,明天在优化。
- package com.example.stepdemp;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.widget.Toast;
- public class Pedometer implements SensorEventListener {
- private SensorManager mSensorManager;
- private Sensor mStepCount;
- private Sensor mStepDetector;
- private float mCount;//步行总数
- private float mDetector;//步行探测器
- private Context context;
- private static final int sensorTypeD=Sensor.TYPE_STEP_DETECTOR;
- private static final int sensorTypeC=Sensor.TYPE_STEP_COUNTER;
- public Pedometer() {
- }
- public Pedometer(Context context) {
- this.context = context;
- mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
- mStepCount = mSensorManager.getDefaultSensor(sensorTypeC);
- mStepDetector = mSensorManager.getDefaultSensor(sensorTypeD);
- }
- public void register(){
- register(mStepCount, SensorManager.SENSOR_DELAY_FASTEST);
- register(mStepDetector, SensorManager.SENSOR_DELAY_FASTEST);
- }
- public void unRegister(){
- mSensorManager.unregisterListener(this);
- }
- private void register(Sensor sensor,int rateUs) {
- mSensorManager.registerListener(this, sensor, rateUs);
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.sensor.getType()==sensorTypeC) {
- setStepCount(event.values[0]);
- }
- if (event.sensor.getType()==sensorTypeD) {
- if (event.values[0]==1.0) {
- mDetector++;
- }
- }
- }
- public float getStepCount() {
- return mCount;
- }
- private void setStepCount(float count) {
- this.mCount = count;
- }
- public float getmDetector() {
- return mDetector;
- }
- }