简体   繁体   English

如何为步骤计数器设置步骤之间的最短时间?

[英]How to set a minimum amount of time between steps for a Step Counter?

I created a step counter and I noticed that you are able to make it count six steps in under a second by shaking the device. 我创建了一个计步器,我注意到您可以通过摇动设备使其在一秒钟内计数六步。 How could you set a minimum amount of time between each step to make it more accurate? 您如何在每个步骤之间设置最短的时间以使其更准确? For example make it so that there has to be at least a quarter of a second that went by before a next step can be counted as a step. 例如,使它至少要经过四分之一秒才能将下一步骤计为一个步骤。 If this isn't a good solution to making it more accurate then please let me know. 如果这不是提高准确性的好方法,请告诉我。

Below is the code, it includes a timer that just counts the total amount of steps every ten minutes, so just ignore that. 下面是代码,它包含一个计时器,该计时器仅每十分钟计算一次总步数,因此请忽略它。

public class StepCounterManager implements SensorEventListener{

boolean timerStarted = false;
private float initCount, finalCount, currentCount;

public Activity activity;
private boolean activityRunning;

private SensorManager sensorManager;

public StepCounterManager(Activity activity){
    this.activity = activity;
}

public void stepCounterInit(){
    sensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
    timer();
    timerStarted = true;
}

//timer starts at the start of app and restarts every 10 minutes
public void timer(){
    Timer t = new Timer(false);
    Toast.makeText(this.activity, "timer started", Toast.LENGTH_SHORT).show();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    getFinalStepCount();
                }
            });
        }
    },  600000);
}

public void register(){
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if(countSensor != null){
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(this.activity, "Count sensor not available", Toast.LENGTH_SHORT).show();
    }
}

public void unRegister(){
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if(activityRunning){
        currentCount = event.values[0];
        System.out.println(currentCount);
    }
    if(timerStarted){
        resetInitialStepCount();
    }
}

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

}

//a new initial count has to be made because the step count can only reset after a reboot
public void resetInitialStepCount(){
    initCount = currentCount;
    Toast.makeText(this.activity, "initial count is: " + initCount, Toast.LENGTH_SHORT).show();
    timerStarted = false;
}

public void getFinalStepCount(){
    finalCount = currentCount-initCount;
    Toast.makeText(this.activity, "final count is: " + finalCount, Toast.LENGTH_SHORT).show();
    resetInitialStepCount();
    timer();
    //todo: send finalcount to database
}

} }

public class StepCounterManager implements SensorEventListener{

boolean timerStarted = false;
private float initCount, finalCount, currentCount;

public Activity activity;
private boolean activityRunning;
private boolean hasRecorded;
private SensorManager sensorManager;
private int storedSteps;

public StepCounterManager(Activity activity){
    this.activity = activity;
    this.hasRecorded = false;
    this.storedSteps = 0;
}

public void stepCounterInit(){
    sensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
    timer();
    timerStarted = true;
}

//timer starts at the start of app and restarts every 10 minutes
public void timer(){
    Timer t = new Timer(false);
    Toast.makeText(this.activity, "timer started", Toast.LENGTH_SHORT).show();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    getFinalStepCount();
                }
            });
        }
    },  600000);
}

public void register(){
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if(countSensor != null){
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(this.activity, "Count sensor not available", Toast.LENGTH_SHORT).show();
    }
}

public void unRegister(){
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
   if(hasRecorded == false){
   hasRecorded = true;
   BlockRecording();
    if(activityRunning){
        int TempCurrent = event.values[0] - storedSteps;
        currentCount = currentCount + (event.values[0] - TempCurrent);
        System.out.println(currentCount);
    }
    if(timerStarted){
        resetInitialStepCount();
    }
   storedSteps = event.values[0];
}else{
}
}


public void BlockRecording(){
Timer t = new Timer(false);
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    hasRecorded = false;
                }
            });
        }
    },  250);
}

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

}

//a new initial count has to be made because the step count can only reset after a reboot
public void resetInitialStepCount(){
    initCount = currentCount;
    Toast.makeText(this.activity, "initial count is: " + initCount, Toast.LENGTH_SHORT).show();
    timerStarted = false;
}

public void getFinalStepCount(){
    finalCount = currentCount-initCount;
    Toast.makeText(this.activity, "final count is: " + finalCount, Toast.LENGTH_SHORT).show();
    resetInitialStepCount();
    timer();
    //todo: send finalcount to database
}
}

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

相关问题 如何以步长和距离计算速度TYPE_STEP_COUNTER - How to calculate speed with steps and distance TYPE_STEP_COUNTER 即使在卸载后,我的计步器也不会重置步数 - My step counter does not reset the steps, even after uninstall 用最少的步骤n(log)N编写Power方法 - Writing a Power method with a minimum amount of steps n(log)N 一次进行 1、2 或 3 步的第 n 步的组合数‽ - Number of combinations to the nth step taking 1, 2 or 3 steps at a time‽ 生成给定字符集在最小和最大字符数之间的所有排列的列表 - Generating a list of all permutations of a given character set between a minimum and maximum amount of characters 动态调整JTextField的大小并设置最小文本量 - Dynamically resize a JTextField and set minimum amount of text 如何从 timepickerdialog 将最小时间设置为系统当前时间 - How to set minimum time to system current time from timepickerdialog 每次启动应用程序时,如何将步数计数器重置为0? - How do I reset my step counter to 0 every time the app launches? 如何设置计数器来读取睡眠时间? - How do i set up a counter to read sleep time? 如何在Android / Java的for循环中等待迭代之间的特定时间? - How to wait for a specific amount of time between iterations in a for loop in Android/Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM