My app is used, amongst other features, for tracking drivers doing delivery work, so we need to track the driver's location constantly. For that, we use a Service that runs in the background and saves the coordinates internally before we send them to a server.
The problem is, as is expected, the more precise we need to get, the more the battery drain increases. To the point where Google Play Console Vitals is showing a way above the average number for wake ups like this: walarm:com.google.android.location.ALARM_WAKEUP_LOCATOR*
I want to know if I'm doing something wrong or if the only way I can improve the code is by doing minor tweaks.
public class LocationService extends Service implements LocationListener {
private Integer gpsFreqInMillis = 1000 * 10; //10 seconds
private Integer gpsFreqInDistance = 10; //10 meters
...
public void startUpdatingLocation() {
...
final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, gpsFreqInMillis, gpsFreqInDistance, this);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
...
@Override
public void onLocationChanged(final Location newLocation) {
//saves location in internal database
}
}
Is there a way I can track a precise location without killing my user's battery? I know I can change the min distance and min time values, but 10m/10s is the most precise option we offer.