Note that there are some explanatory texts on larger screens.

plurals
  1. POstopping Locationmanager is not working in a button
    text
    copied!<p>Hello could somebody help me with what i'm doing wrong. </p> <p>I want this application to work in the background, i just want to make in a button where to stop the locationmanager. when i use the removeUpdates, its not working. I cant call that function there.</p> <pre><code>public class LbsGeocodingActivity extends Activity { private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters private static final long MINIMUM_TIME_BETWEEN_UPDATES = 60000; // Minuten(van milliseconden) * aantal protected LocationManager locationManager; protected LocationListener locationListener; protected Button retrieveLocationButton; protected Button stopLocationButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button); stopLocationButton = (Button) findViewById(R.id.stop_location_button); LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener() ); retrieveLocationButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showCurrentLocation(); } }); retrieveLocationButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LocationManager.removeUpdates(locationListener) ; } }); } protected void showCurrentLocation() { Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { String message = String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() ); Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show(); } } public class MyLocationListener implements LocationListener { public void onLocationChanged(Location location) { String message = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() ); Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show(); } public void onStatusChanged(String s, int i, Bundle b) { Toast.makeText(LbsGeocodingActivity.this, "Provider status changed", Toast.LENGTH_LONG).show(); } public void onProviderDisabled(String s) { Toast.makeText(LbsGeocodingActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show(); } public void onProviderEnabled(String s) { Toast.makeText(LbsGeocodingActivity.this, "Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show(); } } </code></pre> <p>I really need this badly</p>
 

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