java - Android GPS 速度警报

标签 java android performance gps location

我正在创建一个速度警报应用程序,但我在创建过程中卡在了一个点。 我的速度显示正确,但我需要发出警报的设定速度不起作用。 我想我把代码放在了错误的地方。 我的代码是:

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

            } 
          } 
       }

  }

}

最佳答案

根据您发布的代码,我相信您没有调用您的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);

  }
}

希望对您有所帮助。

关于java - Android GPS 速度警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858703/

相关文章:

java - View - 没有零参数构造函数

php - 提高 PHP MYSQL 查询性能或服务器问题?

java - 无法从其他 jsp 上的 session 获取哈希表值

java - 使用 ServerSocket 抛出 ConnectException

java - 在捕获的组上应用正则表达式

Android Sqlite 限制删除

java - 无法添加 OAuth 2.0 范围 LinkedIn

安卓工作室 : Are language xml files compiled into the apk?

java - 如何调节日志指令随时间产生的打印输出量?

python - 如何有效地将字典的条目转换为数据框