Time Android java开发加速时间到

Time Android java开发加速时间到,time,acceleration,Time,Acceleration,如何计算加速到100公里小时的时间? 嗯,我注册了一个位置侦听器,当!location.hasSpeed()为true将定位时间存储到变量中。在这种情况下,当速度达到给定速度100km/h(27.77 m/s)时,我从位置上减去,结果除以1000 这是“伪代码” 我在这里看到的主要问题是,每当设备移动时,startAccelerationToTime就会重置。(第一个if仅检查是否有移动;它不检查是否已记录开始时间 我看不出哪里需要isAccelerationLoggingStarted——速度

如何计算加速到100公里小时的时间? 嗯,我注册了一个位置侦听器,当!location.hasSpeed()为true将定位时间存储到变量中。在这种情况下,当速度达到给定速度100km/h(27.77 m/s)时,我从位置上减去,结果除以1000

这是“伪代码”


我在这里看到的主要问题是,每当设备移动时,
startAccelerationToTime
就会重置。(第一个
if
仅检查是否有移动;它不检查是否已记录开始时间

我看不出哪里需要
isAccelerationLoggingStarted
——速度和变量本身可以稍微清理一下,以明确下一步应该做什么

您的伪代码可能应该如下所示:

if speed is 0
    clear start time
else if no start time yet
    start time = current time
    clear acceleration time
else if no acceleration time yet, and if speed >= 100 mph 
    acceleration time = current time - start time
在Java中,这看起来像

long startTime = 0;
double accelerationTime = 0.0;

@Override
public void onLocationChanged(Location currentLoc) {

    // when stopped (or so slow we might as well be), reset start time
    if (!currentLoc.hasSpeed() || currentLoc.getSpeed() < 0.005) {
        startTime = 0;
    }

    // We're moving, but is there a start time yet?
    // if not, set it and clear the acceleration time
    else if (startTime == 0) {
        startTime = currentLoc.getTime();
        accelerationTime = 0.0;
    }

    // There's a start time, but are we going over 100 km/h?
    // if so, and we don't have an acceleration time yet, set it
    else if (accelerationTime == 0.0 && currentLoc.getSpeed() >= 27.77) {
        accelerationTime = (double)(currentLoc.getTime() - startTime) / 1000.0;
    }
}
long startTime=0;
双加速时间=0.0;
@凌驾
已更改位置上的公共无效(位置currentLoc){
//当停止时(或者我们可能很慢),重置开始时间
如果(!currentLoc.hasSpeed()| | currentLoc.getSpeed()<0.005){
起始时间=0;
}
//我们要走了,但还有开始的时间吗?
//如果不是,则设置并清除加速时间
else if(startTime==0){
startTime=currentLoc.getTime();
加速时间=0.0;
}
//有一个启动时间,但我们的速度是否超过100公里/小时?
//如果是这样的话,我们还没有加速时间,设定它
否则如果(accelerationTime==0.0&¤tLoc.getSpeed()>=27.77){
加速时间=(双精度)(currentLoc.getTime()-startTime)/1000.0;
}
}

现在,我不确定位置侦听器是如何工作的,也不确定它们在您移动时通知您的频率。因此,这可能只起了一半作用。特别是,当您不移动时,
onLocationChanged
可能不会被调用;您可能需要请求更新(可能通过“重置”按钮或其他方式)或者设置某些参数以触发速度=0时发生的事情。

谢谢。但是出现了另一个问题。从开始移动到GPS实现移动和触发onLocationChange事件需要多长时间。我假设这部分取决于设备,部分取决于传递给
RequestLocationUpdate的标准
。我也将零传递给minTime和MindInstance
long startTime = 0;
double accelerationTime = 0.0;

@Override
public void onLocationChanged(Location currentLoc) {

    // when stopped (or so slow we might as well be), reset start time
    if (!currentLoc.hasSpeed() || currentLoc.getSpeed() < 0.005) {
        startTime = 0;
    }

    // We're moving, but is there a start time yet?
    // if not, set it and clear the acceleration time
    else if (startTime == 0) {
        startTime = currentLoc.getTime();
        accelerationTime = 0.0;
    }

    // There's a start time, but are we going over 100 km/h?
    // if so, and we don't have an acceleration time yet, set it
    else if (accelerationTime == 0.0 && currentLoc.getSpeed() >= 27.77) {
        accelerationTime = (double)(currentLoc.getTime() - startTime) / 1000.0;
    }
}