我需要停止位置监听器更新。我知道为了停止我需要调用的位置侦听器,locationManager.removeUpdates(LocationListener)
但我不断收到错误消息MyLocationListener cannot be resolved to a variable
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class GetCoords extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; //in Milliseconds
protected LocationManager locationManager;
protected Button getLocationButton;
protected Button stopLocationButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_coords);
getLocationButton = (Button) findViewById(R.id.get_location_button);
stopLocationButton = (Button) findViewById(R.id.stop_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
getLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
//String message = "GPS Service Started";
Toast.makeText(GetCoords.this, "GPS Service Started", Toast.LENGTH_SHORT).show();
}
});
stopLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopMyLocation();
//locationManager = null;
//stopCurrentLocation();
Toast.makeText(GetCoords.this, "GPS Service Stopped", Toast.LENGTH_SHORT).show();
}
});
}
protected void stopMyLocation() {
locationManager.removeUpdates(MyLocationListener);
locationManager = null;
}
我不明白我做错了什么,我看到的每个关于如何停止 GPS 更新的示例都让我认为我在 stopMyLocation() 下的代码是正确的。我已经在 stopLocationButton 点击监听器中尝试过,在它的当前位置,几乎无处不在,它一直告诉我 MyLocationListener 不正确。