我正在创建一个速度提醒应用程序&我陷入了创作的一个角落。 我的速度正常显示,但我需要提醒的设定速度不起作用。 我想我把代码放在了错误的位置。 我的代码是:
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();
}
}
}
}
}
}
答案 0 :(得分: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);
}
}
我希望这会有所帮助。