2016-07-05 160 views
1

我正在使用CountDownTimer,但似乎沒有正常工作。我需要捕捉速度;如果它高於一定的速度,什麼也不做。我只是在記錄速度。但是如果它以一定的速度下降,我需要在三分鐘倒數計時器後啓動一個通知。使用倒數計時器

有人可以看看我的代碼,看看我做錯了什麼嗎?如果速度超過了,那很好......它不運行計時器,但問題是當它以一定的速度進行時。有時計時器工作,其他時間,不。有時它只記錄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) { 
    } 
} 

這裏的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英里時,這是完美的,沒有計時器。一旦它下降,在5英里每小時,仍然沒有計時器。我將添加現在正在發佈的日誌.. – Angel

+0

我更新了代碼 - 您還需要考慮計時器是否已啓動,因此我重新加入了'timerStarted'。 –

+0

謝謝!這絕對有效。在節目開始時我還有一個問題;當沒有速度時它仍然運行計時器0,但我正在努力解決它。如果我有任何其他問題,我會再次回到這裏。謝謝!!! – Angel