简体   繁体   English

不寻常的步数计数器读数

[英]Unusual step counter reading

I am trying to build a step counter app for android using the accerlerometer of the mobile. 我正在尝试使用移动的accerlerometer为android构建一个步数计数器应用程序。 Without restarting the mobile, it gives almost accurate reading. 无需重新启动移动设备,即可获得几乎准确的读数。 That is for each steps, it increments the value by 1. When the mobile is restarted, the step counter value goes to 0 so i saved the value of the previous reading using the sharedPreferences . 这是针对每个步骤,它将值递增1.当重新启动移动设备时,步数计数器值变为0,因此我使用sharedPreferences保存了先前读数的 But when I start my mobile and start walking, for each steps the step counter increments by 2. How to solve it? 但是当我启动手机并开始行走时,步数计数器每步增加2.如何解决?

public class Pedometer extends Activity implements SensorEventListener {
    private TextView textView;

    private SensorManager mSensorManager;

    private Sensor mStepCounterSensor;

    private Sensor mStepDetectorSensor;

    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pedometer);
        textView = (TextView) findViewById(R.id.textview);

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

    @Override
    public void onSensorChanged(SensorEvent event) {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();

        Sensor sensor = event.sensor;
        float[] values = event.values;
        int value = -1;
        if (values.length > 0) {
            value = (int) values[0];
        }

        int temp =  sharedPreferences.getInt("steps", 0);

        if (temp > value) {
            temp = temp + 1;
            editor.putInt("steps", temp).commit();
        } else {
            editor.putInt("steps", value).commit();
        }

        int count = sharedPreferences.getInt("steps", 0);

        if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            textView.setText("Step Counter Detected : " + count);
        } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
            // For test only. Only allowed value is 1.0 i.e. for step taken
            textView.setText("Step Detector Detected : " + count);
        }
    }

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

    }

    @Override
    protected void onResume() {

        super.onResume();

        mSensorManager.registerListener(this, mStepCounterSensor,

                SensorManager.SENSOR_DELAY_FASTEST);
        mSensorManager.registerListener(this, mStepDetectorSensor,

                SensorManager.SENSOR_DELAY_FASTEST);

    }

    @Override
    protected void onStop() {
        super.onStop();
        mSensorManager.unregisterListener(this, mStepCounterSensor);
        mSensorManager.unregisterListener(this, mStepDetectorSensor);
    }
}

The problem is that your listener is not unregistered, just as some other people reported . 问题是你的听众没有注册,就像其他人报道的那样 Since you have multiple listeners registered and a shared variable, the steps will be counted twice. 由于您已注册多个侦听器和共享变量,因此步骤将计为两次。 To solve this you will need to get a shared state to track if your listener is unregistered. 要解决此问题,您需要获取共享状态以跟踪您的侦听器是否未注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM