26

How to read location only once with locationManager (GPS and NETWORK PROVIDER) and not any more looking for updates of location, to save battery?

Damir
  • 54,277
  • 94
  • 246
  • 365

7 Answers7

34

Although requestSingleUpdate() is the technically correct answer, you should use

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mLocationListener);

Wait for a while after getting your first location. The location tends to wobble for a few seconds. To determine if the fix is stable use, location.getAccuracy(). Once the accuracy stabilizes, call locationManager.removeUpdates(mLocationListener);

Reno
  • 33,594
  • 11
  • 89
  • 102
  • 7
    Could you elaborate further why the above should be used instead of `requestSingleUpdate()` - what are the advantages? thanks – ericn Apr 27 '13 at 08:53
  • 7
    @fuzzy It takes a few seconds for the GPS location to stabilize. The first location's accuracy is usually bad. – Reno Apr 27 '13 at 09:35
  • 3
    how can we wait for few seconds? Should we use sleep? – Bugs Happen Jul 12 '15 at 11:39
  • @BugsHappen keep on requesting until the accuracy is good enough from location.getAccuracy() and then use removeUpdates(). – Johan Bjäreholt Nov 03 '15 at 22:57
  • 2
    When I implement this method, I have to actually move around before it recognizes the location as having had changed. I basically want to be able to get my current GPS coords without having to move first. – Nathan F. Aug 26 '16 at 23:11
  • 2
    How do I check against location's accuracy if it's stable enough? – mr5 Mar 27 '17 at 08:04
  • 1
    This won't update location for 1 time it will update location after move 1 meter or after 1000 millisecond. –  Oct 11 '17 at 23:51
  • 1
    useless answer, it could have been more elaborate... why bother answering a question if you are going to spit it out like this – Caio Mar Aug 03 '18 at 12:50
  • 1
    GPS accuracy is 7.8m for a single band GPS reception with no multipath interference (signal reflection from surfaces) since I gave the answer though there are dual-band gnss signals (from different constellations) so it depends on the topology and how many space vehicles are in direct line of sight. You can get 3.4m accuracy nowadays. What level of accuracy is critical for you is up to your application. Just yesterday the gen 3 satellites were launched. In the next few years watch out for sub metre accuracy :^) – Reno Nov 09 '20 at 16:21
7
public LocationManager locationManager;

try { 
 locationManager.requestSingleUpdate( LocationManager.GPS_PROVIDER, new MyLocationListenerGPS(), null );  
} catch ( SecurityException e ) { e.printStackTrace(); }

public class MyLocationListenerGPS implements LocationListener {
 @Override
 public void onLocationChanged(Location location) {
 // your code and required methods go here...
}

Using 'null' in 'requestSingleUpdate' keeps the update check on the same thread (for use in a service). The method allows for you to put it in a Looper if you're running it from an activity. I would think that using an async task would be the better approach.

VikingGlen
  • 1,705
  • 18
  • 18
7

Just remove updated from the location manager.

locationManager.removeUpdates(this);
vallentin
  • 23,478
  • 6
  • 59
  • 81
Quentin DOMMERC
  • 876
  • 1
  • 8
  • 24
1

Please read Obtaining User Location developer guide : http://developer.android.com/guide/topics/location/obtaining-user-location.html

This link for best Performance

Don't forget to specify android.permission.ACCESS_FINE_LOCATION in the Android manifest

u.jegan
  • 833
  • 6
  • 15
1

To get the location only once you can refer this link and search for the requestSingleUpdate

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
0

Its easy just once read the loacation update like

locationManager.requestLocationUpdates(provider, 400, 1, this);

and after reading the location once called

locationManager.removeUpdates(this);

so system will not ask for location update after this and you cab save your battery.

neeraj t
  • 4,654
  • 2
  • 27
  • 30
0

Using requestSingleUpdate with your LocationManager. For more info, official guide is interesting: INFO

ArcDare
  • 3,106
  • 4
  • 27
  • 38