如何从android重置步数

时间:2016-07-29 04:06:11

标签: java android

public class speedDetection extends Activity implements SensorEventListener {

    private TextView textView;
    private SensorManager mSensorManager;
    private Sensor mStepCounterSensor;
    private Sensor mStepDetectorSensor;
   // boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speed_detection);


        //pass in the level from time to time.
        //int level = this.getIntent().getIntExtra("Level",0);

        textView = (TextView) findViewById(R.id.textView);

        //Toast.makeText(getApplicationContext(), level, Toast.LENGTH_LONG).show();

        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) {

        Sensor sensor = event.sensor;
        float[] values = event.values;
        int stepsInSensor = -1;

        /*if (!flag) {
            //Reset the count when reset the apps
            int initValue = (int) values[values.length - 1];
            stepsInSensor = stepsInSensor - initValue;
        }*/

        if (values.length > 0) {                //some values was inside
            stepsInSensor = (int) values[0];    //the latest value added will be at value[0]
        }

        if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            textView.setText("Step Counter Detected : " + stepsInSensor);

        } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
            textView.setText("Step Detector Detected : " + stepsInSensor);
        }
    }

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

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_FASTEST);
        mSensorManager.registerListener(this, mStepDetectorSensor,SensorManager.SENSOR_DELAY_FASTEST);

    }

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

    }
}

任何人都知道如何在每次应用启动时重置以{0}开头的stepInSensorSensor.TYPE_STEP_COUNTER只能在设备重启时重置。

我发现回答这个问题的帖子就像"将第一个值存储为初始值,然后通过它减去后续值。"

但究竟能做到什么?

2 个答案:

答案 0 :(得分:0)

这只是数学,对吧?

应用程序启动时存储初始步数。当您尝试获取新的步长值时,只需从当前步长传感器值中减去初始值。这可以告诉您自应用程序启动以来已经采取了多少步骤。

答案 1 :(得分:0)

使用以下代码

// return -1 if it is not initialize
int startingStepCount = prefs.getInt(Initial_Count_Key, -1); 
int stepCount = stepsInSensor - startingStepCount;

如何获得步数

Editor editor = prefs.edit();
editor.putInt(Initial_Count_Key, stepsInSensor);
editor.commit();

如何重置(重置起始值)

function perusahaanShow(){
    setTimeout(function() {
        $('#Company').modal();
    }, 450);
}