Periodically receive location (coordinates) without significantly increasing battery consumption

I am developing an Android application; this application should periodically send (every 10 minutes) the current position (coordinates) to the web service. But ... I'm a little confused in a more proper way (and more battery friendly on the device) to do this.

I read this answer and its _getLocation() method looks good; but I don’t know if this method will be able to get a place accessible to me; general availability ...

I would like if this place is not available using GSM / WIFI, the application selects the GPS method.

Is this what this method does?

 private void _getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria criteria = new Criteria(); String bestProvider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(bestProvider); try { lat = location.getLatitude(); lon = location.getLongitude(); } catch (NullPointerException e) { lat = -1.0; lon = -1.0; } } 

Does anyone know one way to get device coordinates periodically ... without significantly increasing battery consumption?

+6
source share
2 answers

If you are worried about the battery and not so strict for a 10-minute interval, you can try using PassiveProvider instead of GPS / Coarse.
Other applications typically request locations often, so you don’t have to worry about that. If you are strict, you can try asking about the location yourself if you haven’t received for the last interval.
Here is an example of using a Passive provider.

 LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) {} @Override public void onProviderEnabled(String provider) {} @Override public void onProviderDisabled(String provider) {} @Override public void onLocationChanged(Location location) { // Do work with new location. Implementation of this method will be covered later. doWorkWithNewLocation(location); } }; long minTime = 10*60*1000; long minDistance = 0; locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, minTime, minDistance, locationListener); 
+4
source

Play services have a low consumption location API. You can find more information on the Android Developer Site.

UPDATE

Here you can find an example of the Play Location Services stored on Github. See an example of LocationUpdates .

When setting up a location request, you can change the priority, see more details here . I think you are using PRIORITY_BALANCED_POWER_ACCURACY

+1
source

Source: https://habr.com/ru/post/981682/


All Articles