延迟android步骤计数器

时间:2016-05-10 10:38:53

标签: android android-sensors

我正在开发一个android步骤计数器,当我按下按钮时开始计数,当我再次按下按钮时停止计数。问题是计数器总是错过我使用android kitkat 4.4中引入的步进计数器传感器的前几个步骤。我也尝试了步进探测器传感器,但结果相同。 我尝试将传感器注册到最快,游戏,正常时更改延迟,我甚至尝试将延迟设置为0但没有任何效果

我有办法解决这个问题吗?

这是一个简单的应用程序,显示我的问题 https://github.com/omaressameldin/simplestepcounter

MainActivity.java

package com.example.oessa_000.simplestepcounter;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mStepCounterSensor;

    ToggleButton countToggle;
    TextView stepView;

    private int stepCount = 0;
    boolean toggle = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);

        stepView = (TextView)  findViewById(R.id.stepView);
         countToggle = (ToggleButton) findViewById(R.id.countToggle);
        ;

        countToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggle = !toggle ;
                if(toggle){
                startListening();
                }
                else{
                    stopListening();
                }
            }
        });
    }
    protected void startListening() {

        mSensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_NORMAL);


    }

    protected void stopListening() {
        mSensorManager.unregisterListener(this, mStepCounterSensor);
    }



    public void onResume() {
        super.onResume();

    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        switch(sensor.getType()) {
            case Sensor.TYPE_STEP_COUNTER:
                if (toggle){
                    stepCount++;
                    stepView.setText("Step Count: " + stepCount);
                }
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.oessa_000.simplestepcounter.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Step Count: 0"
        android:textSize="15dp"
        android:id="@+id/stepView"

        />
    <ToggleButton
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/countToggle"
        android:layout_margin="20dp"
        android:textOff="Not Walking"
        android:textOn="Walking"
        android:layout_below="@+id/stepView"

        />
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.oessa_000.simplestepcounter">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <uses-feature android:name="android.hardware.sensor.stepcounter" />

        </activity>
    </application>

</manifest>

已编辑:已添加代码段

1 个答案:

答案 0 :(得分:0)

我意识到使用预定义的stepCounter传感器进行少量步骤,就像我想要的那样,并不是最佳选择,使用加速度计实现计数器会好很多;然而,由于步数传感器的延迟是忽略误报,因此不建议用于构建健身应用程序或任何需要长时间不间步计数的应用程序。

所以这是我使用加速度计

实现的stepcounter

<强> MainActivity.java

package com.example.oessa_000.testcompass;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

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.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements SensorEventListener{


    // Gravity for accelerometer data
    private float[] gravity = new float[3];
    // smoothed values
    private float[] smoothed = new float[3];
    // sensor manager
    private SensorManager sensorManager;
    // sensor gravity
    private Sensor sensorGravity;
    private double bearing = 0;

    private TextView acc;
    private TextView stepView;
    private TextView thresholdView;
    private SeekBar seek;
    private ToggleButton countToggle;

    private int stepCount;
    private boolean toggle;
    private double prevY;
    private double threshold;
    private boolean ignore;
    private int countdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        acc = (TextView) findViewById((R.id.accelerometer));
        thresholdView = (TextView) findViewById((R.id.thresholdView));
        stepView = (TextView) findViewById((R.id.stepView));
        countToggle = (ToggleButton) findViewById(R.id.countToggle);
        seek = (SeekBar) findViewById(R.id.seek);

        seek.setProgress(0);
        seek.incrementProgressBy(1);
        seek.setMax(40);
        // keep screen light on (wake lock light)

        implementListeners();
    }

    protected float[] lowPassFilter( float[] input, float[] output ) {
        if ( output == null ) return input;
        for ( int i=0; i<input.length; i++ ) {
            output[i] = output[i] + 1.0f * (input[i] - output[i]);
        }
        return output;
    }

    @Override
    protected void onStart() {
        super.onStart();
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        // listen to these sensors
        sensorManager.registerListener(this, sensorGravity,
                SensorManager.SENSOR_DELAY_NORMAL);


    }

    @Override
    protected void onStop() {
        super.onStop();
        // remove listeners
        sensorManager.unregisterListener(this, sensorGravity);
    }







    @Override
    public void onSensorChanged(SensorEvent event) {

        // get accelerometer data
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            // we need to use a low pass filter to make data smoothed
            smoothed = lowPassFilter(event.values, gravity);
            gravity[0] = smoothed[0];
            gravity[1] = smoothed[1];
            gravity[2] = smoothed[2];
            acc.setText("x: "+gravity[0] + " y: " + gravity[1] + " z: " + gravity[2]+ "ignore: "+ ignore + "countdown: "+ countdown);
            if(ignore) {
                countdown--;
                ignore = (countdown < 0)? false : ignore;
            }
            else
                countdown = 22;
            if(toggle && (Math.abs(prevY - gravity[1]) > threshold) && !ignore){
                stepCount++;
                stepView.setText("Step Count: " + stepCount);
                ignore = true;
            }
            prevY = gravity[1];
        }
    }

    public void implementListeners(){
        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                threshold = ((double)seek.getProgress()) * 0.02;
                thresholdView.setText("Threshold: "+ threshold);
            }
        });

        countToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggle = !toggle ;
                if(toggle){
                    stepCount = 0;
                    countdown = 5;
                    ignore = true;
                    stepView.setText("Step Count: " + stepCount);
                }
            }
        });

    }



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.oessa_000.testcompass.MainActivity">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_below="@+id/degree"
        android:id="@+id/accelerometer"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Step Count: 0"
        android:textSize="24dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/stepView"
        android:layout_below="@+id/accelerometer"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_marginBottom="0dp"
        android:layout_below="@+id/stepView"
        android:id="@+id/thresholdView"
        />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seek"
        android:layout_marginTop="0dp"
        android:layout_below="@+id/thresholdView"
        />

    <ToggleButton
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/countToggle"
        android:layout_margin="20dp"
        android:textOff="Not Walking"
        android:textOn="Walking"
        android:layout_below="@+id/seek"

        />
</RelativeLayout>

调整阈值可设置传感器的灵敏度:)

参考:http://nebomusic.net/androidlessons/Pedometer_Project.pdf