Reputation: 124
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.
Upvotes: 0
Views: 2063
Reputation: 602
I too was having the same issue. I think my problem was similar to the issues mentioned above as far as different instances of the listener being created. What I ended up doing was creating a static listener and creating a singleton method to prevent another instance of the listener from being created. The code looks something like this:
static LocationListener mLocationListener = null;
public LocationListener getLocationListener(){
if(mLocationListener==null)
mLocationListener = new MyLocationListener();
return mLocationListener;
}
Then I used it this way
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 2, 0, getLocationListener());
locationManager.removeLocationUpdates(getLocationListener());
You could do the same thing with the locationmanager if necessary but I didnt have too. Hope this helps!
Upvotes: 0
Reputation: 480
You get the error because the stopMyLocation() function passes the unknown variable MyLocationListener to removeUpdates:
locationManager.removeUpdates(MyLocationListener);
To pass an existing variable to removeUpdates you have define an object variable of the MyLocationListener class which is accessible in the onCreate() and the stopMyLocation() function.
private MyLocationListener locationListener;
The locationListener variable can be instantiated in the onCreate() function and has to be used to call the requestLocationUpdates(...) function.
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
locationListener
);
Upvotes: 1
Reputation: 2186
You have initialized MyLocationListener, but not assigned it to a variable to be used later. How would Java know which instance of MyLocationListener class you're referring to in the removeUpdates()
call?
Make a class variable LocationListener abc;
In the onCreate()
method add the line abc = new MyLocationListener(); before you register for updates.
Pass this in your requestLocationUpdates()
method.
call removeUpdates(abc)
in your stopMyLocation()
method.
Upvotes: 1
Reputation: 22306
You need to assign your location listener to a field.. as in
protected MyLocationListener listener;
Then in your onCreate:
listener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
listener);
your unregister will then look like
locationManager.removeUpdates(listener);
Upvotes: 3
Reputation: 86948
Save a reference to your LocationListener:
protected LocationManager locationManager;
protected MyLocationListener myLocationListener = new MyLocationListener();
Change this:
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
myLocationListener
);
Now you can cancel it with:
locationManager.removeUpdates(myLocationListener);
Upvotes: 2