1

我正在创建一个速度警报应用程序,并且我在创建过程中陷入了困境。我的速度显示正确,但我需要警报的设定速度不起作用。我想我把代码放在了错误的位置。我的代码是:

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();
            }

            } 
          } 
       }

  }

}
4

1 回答 1

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);

  }
}

我希望这有帮助。

于 2013-04-08T13:34:48.300 回答