I am using geolocation in android using GPS
and NETWORK
, I have the cycle configured every 2 minutes to send the location, but obviously this consumes a lot of battery, I need some good practice so that the location is constantly sent if the user is not moved.
TrackingModels.positionInfo position = new TrackingModels.positionInfo();
LocationManager locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Location coordinatesGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location coordinatesNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location coordinates;
It is precisely as you comment to wait for some time while getting the value of the "last known location":
From experience, obtaining the last known position I believe that 10 minutes is enough for an application that does not make constant use of GPS.
now there is FusedLocation Provider Api with which battery consumption can be further optimized, where the last known position is also obtained through getLastLocation()
I recommend you download the example from the documentation.