Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 497
Damir Avatar asked Nov 02 '11 11:11

Damir


People also ask

Which method is used to list all the location providers in Android?

They are: gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix.

Which two methods in Android decides the location of the current users using GPS?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API. android. permission.

How will you instant location manager object?

First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.

Which is the correct way to get the access of location manager service in Android?

LocationManager is the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling the getSystemService() method.


3 Answers

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);

like image 150
Reno Avatar answered Nov 10 '22 03:11

Reno


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.

like image 27
VikingGlen Avatar answered Nov 10 '22 02:11

VikingGlen


Just remove updated from the location manager.

locationManager.removeUpdates(this);
like image 36
Quentin DOMMERC Avatar answered Nov 10 '22 02:11

Quentin DOMMERC