Java 如何根据步数计算速度

Java 如何根据步数计算速度,java,android,Java,Android,我有一个步骤计数器应用程序,其中我正在运行一个服务,该服务计算所采取的步骤,然后向片段发送广播,然后在片段上更新。步数计算工作正常,但我想根据步数计算速度。这就是我现在正在尝试的 要获取阶跃计数的接收器: receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int steps = intent.getIntExt

我有一个步骤计数器应用程序,其中我正在运行一个服务,该服务计算所采取的步骤,然后向片段发送广播,然后在片段上更新。步数计算工作正常,但我想根据步数计算速度。这就是我现在正在尝试的

要获取阶跃计数的接收器:

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int steps = intent.getIntExtra(StepCounterService.STEP_INCREMENT_KEY, 0);
        if (firstStepTime.equals("0")) {
            firstStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        } else if (secondStepTime.equals("0")) {
            secondStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        } else {
            firstStepTime = secondStepTime;
            secondStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        }

        updateAllUI(steps);
    }
};
因此,我要做的是,一旦开始获取步骤,我就会看到变量
firstStepTime
是否为空。如果是,我将时间保存在
firstStepTime
变量中。在下一步中,我查看
secondStepTime
是否为空,如果为空,我将该时间保存在
secondStepTime
变量中。 现在,对于接下来的步骤,将更新这两个步骤

public void updateAllUI(int numberOfSteps) {

    if (!(firstStepTime.equals("0")) && !(secondStepTime.equals("0"))) {
        try {
            Calendar c = Calendar.getInstance();
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
            timeDifference = timeFormat.parse(secondStepTime).getTime() - timeFormat.parse(firstStepTime).getTime();

            speed = (float) ((0.0254 / (timeDifference * 0.001)) * 3.6);
        } catch (Exception e) {
            timeDifference = 0;
            speed = 0;
        }

        textview.settext(speed +"Km/h);
    }
}
所以在这里,我只是检查两者是否都不是空的,我取值并计算时间差。这里的问题是有时它不能正确计算速度。更大的问题是,如果用户停止,速度保持不变,不会降到零


有没有更好的方法来计算速度?

理想情况下,计算速度的常用方法是使用Location.getSpeed(),或者对加速计值进行一些分析。但我假设你想得到它的基础上计算的步骤

解决其中一个问题:“如果用户停止,速度保持不变,不会降到零。”

您可以使用Android活动识别Api查看用户当前的活动。使用

ActivityRecognition.getMostProbableActivity()
如果检测到的活动类型为STILL,则可以将速度设置为零

你也可以获得自信


对于另一个问题,当你说它不能正确计算速度时,你能详细说明这个问题吗?

因为你只计算步数,并且只想计算步数,所以我假设你不想使用GPS计算速度,或者如果GPS不存在,你想提供备用速度计数器

您可以通过执行以下操作启动新线程

    int interval=1*1000; //specifying interval to check steps
    Handler.postDelayed(mySpeedChecker(),1000);
你需要在每一步都这样做

public void mySpeedChecker(){
// assuming steps taken stored in a global variable
// and there is one more variable which is updated by this function 
// to store number of steps before as global variable to calculate number of steps in time interval. 
// this should check if another step has been taken in the interval to check
// if stepsTaken>0 then speed is not zero and you'll know how much steps are taken
// if zero steps are taken then speed is zero 
// also it will tell you number of steps taken in last second 
// so you can use this to calculate and update speed 
// increase the interval for high accuracy but lower frequency of updates 
}

此外,当GPS可用时,计算用户步幅也是一个好主意。因此,无论用户何时使用GPS,都可以通过在GPS的帮助下找出所覆盖的距离/所采取的步数来计算步长,这将有助于您的系统在所采取的步数的帮助下准确计算用户所覆盖的距离

来自传感器的每个信息都有大量的错误,甚至数据不完整

您应该将收到的信息存储在队列或数据库中,包括时间戳

您将定期选取数据并进行分析,包括过滤以获得更准确的信息


希望能有所帮助。

如果您正在使用FineLocation,那么在谷歌地图api中使用getSpeed应该不会太难。我使用api制作了一个spedometer,工作正常,但有点延迟,有时当用户的移动速度不超过1英里/小时时会关闭。这0.0254将是每一步的固定距离?什么是3.6?还有,你是如何收集时间戳的?是否使用加速计?只有在调用
updateAllUI
时,即在接收器变量调用其
onReceive
方法时,才会显示或更新速度。所以我的问题是,什么时候叫它?它是在每一步之后调用的(这可以解释为什么当一个人停止时它不会更新为0),还是有一个设定的时间间隔(在这种情况下,您希望有一个“超时”窗口将速度更新为0)?旁注:为什么要在catch中更新变量?你不应该处理手头的异常情况吗?我是遗漏了什么,还是根本不可能?要获得速度测量值,您需要将行驶的距离除以到达目的地所需的时间(即
速度=距离/时间
)。您正在使用的
StepCounterService
似乎只提供了两个步骤之间的时间,而不是步骤的距离;那么你怎么可能计算出速度呢?