2016-01-04 23 views
2

我已經計算了車輛的速度並將該速度與某個閾值進行比較,即速度< = 3,現在問題是計算等待時間。 我想計算車輛等待時間,如果汽車停留時間超過兩分鐘超過總時間等待超過兩分鐘後將被添加到等待時間..我使用處理程序和可運行內onLocationchanged() 但我的時間計算是隨機。不同的處理程序和可運行程序正在運行,並且時間比定義的要快。無法放置處理程序和內部可運行OnLocationChanged()

@Override 
public void onLocationChanged(Location location) { 
    diaplayViews(); 


    timeGpsUpdate = location.getTime(); 
    float delta = (timeGpsUpdate - timeOld)/1000; 


    if (location.getAccuracy() <100) { 

     distance += locationOld.distanceTo(location); 


     Log.e("location:", location + ""); 




     speed = (long) (distance/delta); 


     if(speed<=3) 

     { 
      if(runable==null) 
      { 


      runnable = new Runnable() { 

       public void run() { 
        waitingTime++; 
        updateHandler.postDelayed(this, 10000); // determines the execution interval waiting time addition on every 10 seconds after 2 minutes initially 

       } 

      };} 

      updateHandler.postDelayed(runnable, 60*2*1000); // acts like the initial delay 
     } 
     else { 
      handler.removeCallbacks(runable); 

      runable=null; 
     } 

     locationOld = location; 
     timeOld = timeGpsUpdate; 
     diaplayViews(); 

    } 

} 

回答

3

如果使用(location.getspeed()== 0.0)和運行的處理程序每​​隔10秒上和添加兩個分鐘(2 * 60)最初和用於其它時間保持上並稱時間,即10秒,最後60.通過劃分它,你會得到 最好的辦法是使用服務將運行在後臺PROGRAMM並使用廣播接收器通知用戶或在用戶界面中顯示的結果.. http://developer.android.com/guide/components/services.html 和廣播接收器 http://developer.android.com/reference/android/content/BroadcastReceiver.html

相關問題