6

由于更新之间的最短时间,它会在 1 秒后一次又一次地显示位置更新,一旦我得到位置更新,我怎么能停止这个

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) {}
}
4

3 回答 3

6
locationManager.removeUpdates(locationListener);
于 2012-07-03T13:04:14.470 回答
2

要删除更新,您必须致电

locationManager.removeUpdates(<yourLocationListener>);
于 2012-07-03T13:05:19.397 回答
0

Try this :

 locationManager.removeUpdates(locationListener);
 locationManager= null;
于 2012-07-03T13:09:06.100 回答