2013-04-07 36 views
1

我正在創建一個速度提醒應用程序&我一直在創作的一點。 我的速度正常顯示,但我需要警報的設置速度不起作用。 我想我已經把代碼放在了錯誤的地方。 我的代碼是:Android的GPS速度提示

if(insetSpeed > intspeed); 
{ 
    Toast.makeText(speedalert.this, "Over Speed!!", Toast.LENGTH_SHORT).show(); 
} 

所有變量都被轉換爲相同的int類型。

只需要放置上面的代碼。

非常感謝:d

編輯

public void updateSpeed(Location location) 
{ 
float gpsSpeed = 0; 

if(location!=null) 
{ 
    gpsSpeed = ((location.getSpeed()*3600)/1000); 
} 

intspeed = (int)gpsSpeed; 

TextView intSpeedTV = (TextView) findViewById(R.id.intSpeedTV); 
intSpeedTV.setText(String.valueOf(strCurrentSpeed)); 

TextView SpeedTextView = (TextView) this.findViewById(R.id.SpeedTextView); 

SpeedTextView.setText(String.valueOf(intspeed)) 

} 

public void onLocationChanged(Location location) 
{ 
if (location != null) 
{ 
    Location myLocation = new Location(location); 
    this.updateSpeed(myLocation); 

    } 
} 

public void speedAlert(Location location) 
{ 
    if(intspeed >= 2) ; 
    { 
     Toast.makeText(SpeedAlert.this, "Over Speed!!", Toast.LENGTH_SHORT).show(); 
    } 
} 

編輯2

改寫了代碼:

public class SpeedAlert extends Activity 
{ 
    NumberPicker np; 
    int setSpeed; 
    String strsetSpeed; 
    int intspeed; 
    int intsetSpeed; 
    LocationListener LocLis; 
    LocationManager LocMan; 

    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.speedalertmain); 

    LocMan = (LocationManager) getSystemService (Context.LOCATION_SERVICE); 
    LocLis = new GetSpeeed(); 
    LocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, LocLis); 


    np = (NumberPicker) findViewById(R.id.numberPicker1); 
    final TextView setSpeedTV = (TextView) findViewById(R.id.setSpeedTV); 


    np.setMinValue(0); 
    np.setMaxValue(160); 
    np.setWrapSelectorWheel(true); 

    np.setOnValueChangedListener(new OnValueChangeListener() { 

      @Override 
      public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 
       // TODO Auto-generated method stub 

       String New = ""; 

      setSpeedTV.setText(New.concat(String.valueOf(newVal))); 
      newVal = setSpeed; 
      intsetSpeed = Integer.valueOf(setSpeed); 
      } 
    }); 
    } 

    private class GetSpeeed implements LocationListener 
    { 
    public void onLocationChanged(Location location) { 
     if(location!=null) { 
      float gpsSpeed = 0; 
      if(location.hasSpeed()){ 

      gpsSpeed = ((location.getSpeed()*3600)/1000); 

      intspeed = (int)gpsSpeed; 

      TextView SpeedTextView = (TextView) findViewById(R.id.SpeedTextView); 
      SpeedTextView.setText(String.valueOf(intspeed)); 

      TextView intSpeedTV = (TextView) findViewById(R.id.intSpeedTV); 
      intSpeedTV.setText(String.valueOf(intspeed)); 

      if(intspeed >= intsetSpeed) ; 
      { 
       Toast.makeText(SpeedAlert.this, "Over Speed!!", Toast.LENGTH_SHORT).show(); 
      } 

      } 
      } 
     } 

    } 

} 
+1

您的上述代碼確實有意義,請張貼更多代碼,以便我們都能更好地驗證您要實現的目標以及爲什麼它無法正常工作。 – 2013-04-07 08:42:41

+0

感謝您回覆更新的主文章,代碼爲 – gozy 2013-04-08 01:20:19

+0

爲什麼'speedAlert'方法需要一個Location對象?爲什麼'onLocationChanged'方法創建一個名爲'myLocation'的新對象,它不能只調用'updateSpeed(location)'(和'speedAlert()')?另外,考慮速度單位而不是以後。 – Penfold 2013-04-08 15:09:40

回答

1

從你的代碼已經發布,相信喲你不打電話給你public void speedAlert(Location location)。我建議您更新public void onLocationChanged(Location location)像這樣:

public void onLocationChanged(Location location) 
{ 
if (location != null) 
{ 
    // Do you have to create a new Location instance? 
    Location myLocation = new Location(location); 
    this.updateSpeed(myLocation); 
    this.speedAlert(myLocation); 

    } 
} 

我希望這有助於。

+0

好吧,我已經嘗試過。沒有成功。只要gps得到修復,它就會顯示烤麪包。即時將要更新我的原始文章與更新的代碼。我真的很想把這個整理出來,真是令人沮喪。感謝您的幫助:D – gozy 2013-04-12 08:38:45