Note that there are some explanatory texts on larger screens.

plurals
  1. POStopping a Location Listener
    primarykey
    data
    text
    <p>I Need to stop the location listener updates. I know that in order to stop a location listener I need to call <code>locationManager.removeUpdates(LocationListener)</code> but I keep getting an error of <code>MyLocationListener cannot be resolved to a variable</code></p> <p>Here is the code I have:</p> <pre><code>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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload