I Need to stop the location listener updates. I know that in order to stop a location listener I need to call locationManager.removeUpdates(LocationListener) but I keep getting an error of MyLocationListener cannot be resolved to a variable
Here is the code I have:
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;
}
I don’t understand what I am doing wrong, every example I have seen of how to stop GPS updates makes me think that my code under stopMyLocation() is correct. I have tried it inside the stopLocationButton click listener, in its current location, pretty much everywhere and it keeps telling me that MyLocationListener is incorrect.
Save a reference to your LocationListener:
Change this:
Now you can cancel it with: