Search code examples
androidgpsandroid-wifibattery-saver

How to save battery, when use GPS, Wi-Fi in my app?


I develop app, which in service get location and send it to server. Service in background get location every 5 min(for example). Battery quickly dies, when GPS and Wi-Fi uses.. So, how can I save battery life ?

Thanks for help in advance!

Small question: Am I right to do request location updates every 5min code below? And is it correctly use requestLocationUpdates with NETWORK_PROVIDER and next with GPS_PROVIDER ? That necessary, when GPS not find signal, but Wi-Fi find signal and give coordinates. I do this:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, locationListener);

Solution

  • First of all, you do have to realize that these functions that you need to use are probably one of the most demanding in terms of power. I've compiled a few things that might help you consume less power:

    First of all see if you really need the accuracy GPS_PROVIDER. If not choose NETWORK_PROVIDER.

    From what I see you set the requestLocationUpdates callback method to be called 2 times, by setting the NETWORK_PROVIDER and the GPS_PROVIDER. You do not need to use them both. Plus take a look at the parameters that you pass on the requestLocationUpdates method:

    `public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)`
    

    According to the documentation the minDistance is minimum distance between location updates, in meters and you have set it in 0. That means that the device will constantly acquire the users location.