我正在尝试测量单击开始按钮时的行程量,并在单击停止按钮后停止测量。为此 (1) 我已经设置了位置管理器、位置监听和位置对象,并且在我安装 onClick 监听器之前它们已经运行良好。(2) 我设置了onClickListener 后,GPS 不再定期更新。有谁知道如何在事件触发后让 GPS 定期更新?以下是我的代码。
public class MeasureDistance extends Activity implements LocationListener {
TextView showData;
Button start;
Button stop;
double longitutde;
double previousLongitude;
Location previous;
Location current;
LocationListener myListener;
Location location;
Location previousLocation = null;
LocationManager locationManager;
static double meter = 0;
double distance = 0;
boolean startGPS = false;
double diff;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ //This is for the UI
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showData = (TextView)findViewById(R.id.showText);
start = (Button)findViewById(R.id.startButton);
stop = (Button)findViewById(R.id.stopButton);
start.setOnClickListener(onClickListener);
stop.setOnClickListener(onClickListener);
//This is for the Location
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,0,0,this);
}
public void onLocationChanged(Location location)
{
if((previousLocation != location) && (previousLocation != null) &&(startGPS == true))
{
distance = location.distanceTo(previousLocation);
meter = distance + meter;
}
previousLocation = location;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void onProviderEnabled(String provider) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void onProviderDisabled(String provider) {
//To change body of implemented methods use File | Settings | File Templates.
}
public View.OnClickListener onClickListener = new View.OnClickListener()
{
public void onClick(final View v){
switch(v.getId())
{
case R.id.startButton:
{
startGPS = true;
showData.setText(Double.toString(meter * 3.28));
break;
}
case R.id.stopButton:
{
showData.setText("Stop Button is Click");
break;
}
}
}
//To change body of implemented methods use File | Settings | File Templates.
} ;
}