gpt4 book ai didi

java - 如何设置计步器的步数之间的最短时间?

转载 作者:行者123 更新时间:2023-12-02 12:02:13 25 4
gpt4 key购买 nike

我创建了一个计步器,我注意到您可以通过摇动设备使其在一秒内数出六步。如何设置每个步骤之间的最短时间以使其更加准确?例如,必须至少经过四分之一秒才能将下一步计为一步。如果这不是使其更准确的好解决方案,请告诉我。

下面是代码,它包含一个计时器,每十分钟计算一次总步数,所以忽略它。

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
}
}

关于java - 如何设置计步器的步数之间的最短时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47162207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com