2016-07-05 4 views
1

CountDownTimer을 사용하고 있지만 올바르게 작동하지 않는 것 같습니다. 속도를 포착해야합니다. 그것이 특정 속도 이상으로 올라간다면, 아무 것도하지 마라. 나는 단지 속도를 기록하고있다. 그러나 일정한 속도 이하로 내려 가면 3 분 카운트 다운 타이머가 지나면 알림을 표시해야합니다.카운트 다운 타이머 사용

누군가 내 코드를보고 내가 잘못하고있는 것을 볼 수 있습니까? 속도가 위의 경우, 그것은 괜찮아요 .. 그것은 타이머를 실행하지 않지만 문제는 그것이 특정 속도 이하로가는 것입니다. 때로는 타이머가 작동하지만, 다른 때는 아니오입니다. 때로는 단지 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) { 
    } 
} 

는 여기에 내가 얻을 로그 캣의 일부입니다 :

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