Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove the location updates

Tags:

android

Because of min time betwwen update it display the location update after 1 sec again and again how can i sttop this once i get the location update

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1*1000*60; // in Milliseconds
protected LocationManager locationManager;


LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();


locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener);



private 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(),location.getTime()
             );
             Toast.makeText(MyService.this, message, Toast.LENGTH_LONG).show();
         }

         public void onStatusChanged(String s, int i, Bundle b) {}

         public void onProviderDisabled(String s) {}

         public void onProviderEnabled(String s) {}
}
like image 888
Sumit Avatar asked Jul 03 '12 13:07

Sumit


2 Answers

locationManager.removeUpdates(locationListener);
like image 99
nhaarman Avatar answered Oct 08 '22 06:10

nhaarman


For removing updates you have to call

locationManager.removeUpdates(<yourLocationListener>);
like image 21
Simon Dorociak Avatar answered Oct 08 '22 07:10

Simon Dorociak