作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个计步器,我注意到您可以通过摇动设备使其在一秒内数出六步。如何设置每个步骤之间的最短时间以使其更加准确?例如,必须至少经过四分之一秒才能将下一步计为一步。如果这不是使其更准确的好解决方案,请告诉我。
下面是代码,它包含一个计时器,每十分钟计算一次总步数,所以忽略它。
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/
我不确定如何根据输入大小 N 确定运行时间,尤其是当它进入具有某些限制的循环时。这就是我尝试过的。我猜常数是正确的。它看起来如何? i = 1;
我想检索 1 小时前的步数。我不需要做任何特别的事情,我只需要知道用户自上一小时以来完成了多少步。 即使我的 iPhone 记录了一些步数,检索步数的查询返回“nil”。 这是代码: let c
我正在为 Android Wear 开发一款面部 watch 。我想读取 watch 计算的步数(显示在卡上的步数)。是否有任何简单的方法来访问数据?我的研究表明我可以创建自己的步数计数器(由于准确性
我是一名优秀的程序员,十分优秀!