2016-11-25 17 views
0

在過去的一週之間的旅行時間,我一直在使用Android Studio來編寫實現以下目標代碼:問題得到兩分

  1. 等待用戶可在一定開始航點
  2. 一旦在開始航點的距離,即開始記錄GPS數據
  3. 當最終航點越過

目前

  • 停止定時器,我有一個計時器和當前時間開始和結束的路標硬編碼,但我似乎遇到了一個錯誤,我一直試圖跟蹤我的IDE中的逐步功能,但似乎無法找到它。下面是我一直使用的代碼:這使我相信,有不滿意的循環

    void StartTimer (View view){ 
         //Location l = null; 
         boolean hasLoc = false; //are we at the start? 
         float speed = 0; 
         float topSpeed = 0; 
    
    
         while(hasLoc == false && cancel == false){ 
          float d = l.distanceTo(t); 
    
          if(d < 2.0) 
           hasLoc = true; 
    
          //if(!l.equals(lm.getLastKnownLocation(""))) 
          String msg = "Latitude: " + l.getLatitude() + "\nLongitude: "+ l.getLongitude(); 
          Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); 
         } 
    
         hasLoc = false; 
    
         Handler handler = new Handler(); 
         handler.postDelayed(new Runnable() { 
          public void run() { 
           // Actions to do after 10 seconds 
           buzzer(); 
          } 
         }, 10000); 
    
         while(l.distanceTo(tf) > 2.0 && cancel == false){ 
          float cSpeed = l.getSpeed(); 
    
          if(cSpeed>topSpeed) 
           topSpeed = cSpeed; 
    
          String msg = "Current Speed: "+cSpeed+"Top Speed: "+topSpeed; 
          Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); 
         } 
    
         cancel = false; 
        } 
    

    當我運行代碼,手機我測試了一個將運行它,但它不會迴應,我沒有考慮。

    任何建議將是有益的,預先感謝您的意見!

  • 回答

    1

    while while循環阻塞了CPU的執行,導致它不響應。相反,你應該把你的代碼放到一個線程中,並在線程內調用Thread.sleep(1000);,這樣while循環在代碼執行完成後暫停1秒鐘。

    事情是這樣的:

    new Thread(new Runnable() { 
         @Override 
         public void run() { 
    
          while (hasLoc == false && cancel == false) { 
           float d = l.distanceTo(t); 
           if (d < 2.0) 
            hasLoc = true; 
           String msg = "Latitude: " + l.getLatitude() + "\nLongitude: " + l.getLongitude(); 
    
           try { 
            Thread.sleep(1000); 
           } catch (InterruptedException e) { 
            e.printStackTrace(); 
           } 
           runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); 
            } 
           }); 
          } 
    
          hasLoc = false; 
    
          new Handler().postDelayed(new Runnable() { 
           public void run() { 
            // Actions to do after 10 seconds 
            buzzer(); 
           } 
          }, 10000); 
    
          while (l.distanceTo(tf) > 2.0 && cancel == false) { 
           float cSpeed = l.getSpeed(); 
    
           if (cSpeed > topSpeed) 
            topSpeed = cSpeed; 
    
           String msg = "Current Speed: " + cSpeed + "Top Speed: " + topSpeed; 
           try { 
            Thread.sleep(1000); 
           } catch (InterruptedException e) { 
            e.printStackTrace(); 
           } 
           runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); 
            } 
           }); 
          } 
    
          cancel = false; 
    
         } 
        }).start();