Java 使用倒计时

Java 使用倒计时,java,android,countdowntimer,Java,Android,Countdowntimer,我正在使用一个倒计时装置,但它似乎工作不正常。我需要捕捉速度;如果超过一定速度,什么也不要做。。我只是在记录速度。但如果它以一定的速度运行,我需要在三分钟倒计时后发出通知 有人能看看我的代码,看看我做错了什么吗?如果速度超过这个值,就可以了。。它不运行计时器,但问题是当它以一定的速度运行时。有时计时器工作,有时不工作。有时它只记录0,不显示计时器 我知道它的条件,我只是看了一遍又一遍,我看不出什么可能是错误的。四只眼睛比两只好 感谢您的帮助。谢谢 public class SpeedManage

我正在使用一个
倒计时装置
,但它似乎工作不正常。我需要捕捉速度;如果超过一定速度,什么也不要做。。我只是在记录速度。但如果它以一定的速度运行,我需要在三分钟倒计时后发出通知

有人能看看我的代码,看看我做错了什么吗?如果速度超过这个值,就可以了。。它不运行计时器,但问题是当它以一定的速度运行时。有时计时器工作,有时不工作。有时它只记录0,不显示计时器

我知道它的条件,我只是看了一遍又一遍,我看不出什么可能是错误的。四只眼睛比两只好

感谢您的帮助。谢谢

public class SpeedManagerService extends Service implements IBaseGpsListener {

    private static final String TAG = "SpeedCheckerService";
    public boolean vehicleStopped = false;
    public boolean timer_started = true;

    float nCurrentSpeed = 0;

    CountDownTimer timer = new CountDownTimer( 180000, 1000 ) {

        // If speed increases again, cancel timer.
        public void onTick(long millisUntilFinished) {
            Log.i( "Current Ride", "Timer countdown: " + millisUntilFinished / 1000 +
                    " seconds." );
            if (vehicleStopped) {
                // Vehicle should have stopped, but it has started moving again
                if (nCurrentSpeed > 0) {
                    timer.cancel();
                    timer_started = false;
                }
            } else if (nCurrentSpeed == 0) {
                // If vehicle has just slowed down,
                // once speed drops to zero we have stopped
                vehicleStopped = true;
            }
        }

        public void onFinish() {
            Intent resultIntent = new Intent( SpeedManagerService.this, CurrentRide.class );
            NotificationCompat.Builder builder = new NotificationCompat
                    .Builder( SpeedManagerService.this );
            builder.setContentText( "Click to save Current Ride info" );
            builder.setSmallIcon( R.mipmap.ic_launcher );
            builder.setContentTitle( "Did you just pay for a Ride?" );
            builder.setContentIntent( PendingIntent.getActivity( SpeedManagerService.this, 0,
                    resultIntent, 0 ) );
            NotificationManagerCompat.from( SpeedManagerService.this ).notify( 0,
                    builder.build() );
            timer.start();
        }
    }
            .start();

    @Override
    public void onCreate() {
        Log.i( TAG, "in onCreate()" );

        LocationManager locationManager = (LocationManager) this.
                getSystemService( Context.LOCATION_SERVICE );
        if (ActivityCompat.checkSelfPermission( this, android.Manifest.permission
                .ACCESS_FINE_LOCATION )
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this,
                android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager
                .PERMISSION_GRANTED) {

            return;
        }
        locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, this );
        SpeedManagerService.this.updateSpeed( null );
    }

    // On start, run speed service, and return sticky so if error, service will restart
    // on its own
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand( intent, flags, startId );
        Log.i( TAG, "Service onStartCommand" );
                if (nCurrentSpeed == 0) {
            timer_started = false;
        }
        updateSpeed( null );
        return Service.START_STICKY;
    }

    public void updateSpeed(SpeedLocation location) {
        Log.i( "Current Ride ", "User is in a Vehicle. Speed is: " +
                Math.round( nCurrentSpeed ) + " mph. " );

        // If a location exists, get speed
        if (location != null) {
            nCurrentSpeed = location.getSpeed();
        }

        // In meters/second, if speed goes above 8 mph, then it will just log the
        // speed as miles/hour.
        if (nCurrentSpeed >= 8) {
        }

        // However, if speed falls below 5 mph, then countdown timer
        // of 3 minutes will begin.
        if (nCurrentSpeed <= 5 && !timer_started) {
            // Flag to indicate if vehicle has come to a complete stop
            vehicleStopped = (nCurrentSpeed == 0);
            // Indicate the timer is running
            timer_started = true;
            timer.start();
        }
    }

    @Override
    public void onDestroy() {

        timer.cancel();
        Log.i( TAG, "Timer cancelled" );

        super.onDestroy();
    }

    // Binder returns null because it's not used
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    // If location changes, update location and speed.
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            SpeedLocation myLocation = new SpeedLocation( location, false );
            this.updateSpeed( myLocation );
        }
    }

    // If provider is disabled, timer won't run either
    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onGpsStatusChanged(int event) {
    }
}

在显示下一个里程数之前,它会这样做25次。

根据您的评论判断,您需要制定一种稍微复杂一点的确定停车的方法。在启动计时器时,可能需要这样做:

    if (nCurrentSpeed <= 5 && !timerStarted) {
        // Flag to indicate if vehicle has come to a complete stop
        vehicleStopped = (nCurrentSpeed == 0);
        // Indicate the timer is running
        timerStarted = true;
        timer.start();
    }

上述内容似乎满足了您的要求。

您需要自己设置速度(),还应该使用
hasSpeed()
来验证是否有可用的速度。此外,如果
location==NULL
nCurrentSpeed
没有更新,那么您可能使用了一个错误的值。所以我将其设置为开始捕获所需的速度?位置。设定速度((浮动)8)?有关如何处理
getSpeed()
的更多详细信息,请参阅。我太困惑了。在我的OnLocationChanged()下,我正在检查位置。我对位置没有问题,甚至当速度增加时,倒计时器也不会运行…这可能是一个愚蠢的问题,但我如何才能使应用程序仅在nCurrentSpeed>=8时启动?也许这就是我的问题所在,为什么它以0的速度运行?我真的认为这会起作用,因为这是我之前的一些改变。。。逻辑是有道理的,但计时器仍然不工作。当我超过每小时8英里时,它是完美的,没有计时器。一旦下降到5英里/小时以下,仍然没有计时器。我将添加即将发布的日志。我已经更新了代码-您还需要考虑计时器是否已启动,因此我在
timerStarted
中添加了回去。谢谢!这确实奏效了。在节目开始时,我还有一个问题;当没有速度时,它仍然以0的速度运行计时器,但我正在努力解决这个问题。。如果我还有其他问题,我会再发回来。非常感谢。我想出来了。我取消了onStart命令上的计时器,因此它现在运行正常:)因此它工作了几次,然后停止工作。为什么?现在,它所做的就是再次运行计时器。我再次编辑了服务类。。。我做错了什么,它不能一直呆着做事情,不,只是有时候/
    if (nCurrentSpeed <= 5 && !timerStarted) {
        // Flag to indicate if vehicle has come to a complete stop
        vehicleStopped = (nCurrentSpeed == 0);
        // Indicate the timer is running
        timerStarted = true;
        timer.start();
    }
    public void onTick(long millisUntilFinished) {
        Log.i( "Current Ride", "Timer countdown: " + millisUntilFinished / 1000 +
                " seconds." );
        if (vehicleStopped)
        {
            // Vehicle should have stopped, but it has started moving again
            if (nCurrentSpeed > 0)
            {
                timer.cancel();
                timerStarted = false;
            }
        }
        else if (nCurrentSpeed == 0)
        {
            // If vehicle has just slowed down,
            // once speed drops to zero we have stopped
            vehicleStopped = true;
        }
    }