2016-07-05 9 views
1

私はCountDownTimerを使用していますが、正しく動作していないようです。私はスピードを取る必要があります。もしそれが一定のスピードを上回るなら、何もしないでください。しかし、それが一定のスピードを下回っているなら、私は3分のカウントダウンタイマーの後に通知を発する必要があります。CountDown Timerの使用

誰かが自分のコードを見て、私が間違っていることを見ることができますか?速度が上に行くなら、それはいいです..それはタイマーを実行しませんが、問題は一定の速度の下に行くときです。タイマーが動作することもあります。場合によっては0を記録するだけで、タイマーは表示されません。

私はそれが条件にあることを知っています、私は何度も何度もそれを見てきました。何が間違っているのか分かりません。 4つの目は2つよりも優れていますか?

何か助けていただければ幸いです。ありがとう。

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

は、ここで私が得るlogcatの一部です:

07-05 17:42:17.742 17023-17023/ I/Current Ride:: User is in a Vehicle. Speed is: 3 mph. 
07-05 17:42:17.752 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.752 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.762 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 
07-05 17:42:17.772 17023-17023/ I/Current Ride: Timer countdown: 179 seconds. 

し、次の走行距離が表示される前に、25回のようにこれを行います。

+0

自分で 'setSpeed()'を実行する必要があります。また、 'hasSpeed()'を使って速度を確認する必要があります。さらに、 'location == NULL'の場合、' nCurrentSpeed'は更新されないので、不正な値を使用している可能性があります。 –

+0

キャプチャを開始するのに必要な速度に設定しましたか? location.setSpeed((float)8); ? – Angel

+0

'を処理する方法の詳細については、[この質問](https://stackoverflow.com/questions/11843801/find-out-the-speed-of-the-user-in-android-using-gps)を参照してください。 getSpeed() ' –

答えて

1

あなたのコメントで判断すると、やや洗練された停止方法を判断する必要があります。そして、すべてのダニを

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

は、上記の要件を満たしているようです。

+0

私は本当にこれがうまくいくと思っていました。なぜなら、それは私が持っていたものからいくつかの変更があったからです...そして論理は理にかなっていますが、タイマーはまだ動作していません。私が8マイルを過ぎて行くと、それは完璧です。タイマーはありません。一度それが落ちたら、5mphの下で、まだタイマーはありません。 – Angel

+0

コードを更新しました。また、タイマーが開始したかどうかを考慮する必要があるので、 'timerStarted'に追加しました。 –

+0

ありがとう!これは間違いなく働いた。私はまだプログラムの始めにもう一つ問題があります。それは速度がないときに0でタイマーを実行しますが、私はそれを解決しようとしています..私は他の質問があれば、私はここに再び投稿します。ありがとうございました!!! – Angel

関連する問題