Reputation: 92
I am working on a android project where i have to get the current location of the user for every 2 minutes and the code we are using is
try {
DpGPSEnabled = DpLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
DpNetworkEnabled = DpLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (DpIsGpsProvider && DpGPSEnabled) {
DpLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,DpMIN_TIME_BW_UPDATES,DpMIN_DISTANCE_CHANGE_FOR_UPDATES,DpLocationListener);
} else if (!DpGPSEnabled) {
DpLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,DpMIN_TIME_BW_UPDATES,DpMIN_DISTANCE_CHANGE_FOR_UPDATES,DpLocationListener);
DpGenUtil.setLocationPostion(DpLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
}
if (DpGPSEnabled && DpNetworkEnabled) {
DgProvider = "GPS+Network";
} else if (DpNetworkEnabled) {
DgProvider = "Network";
} else if (DpGPSEnabled) {
DgProvider = "GPS";
} else {
DgProvider = "Not Selected";
}
} catch (Exception ex) {
}
where DpMIN_TIME_BW_UPDATES =1000*60*2; 2 minutes and DpMIN_DISTANCE_CHANGE_FOR_UPDATES = 200; in meters
the problem is we are able to get the locations properly , but battery started to drain faster , is there any other method we can get the location and it will not consume much battery also.
Upvotes: 1
Views: 455
Reputation: 66649
Gps consumes more battery than wifi. You can request location updates using Criteria with LocationManager or you can use FusedLocationProvider api if not having Google Play Services is not an issue. FLP uses Wifi, Gps and Kalman filters for location accuracy. You can test both LocationManager and FLP for accuracy and battery consumption. I get up to 4m accuracy(better devices may get better results) on outdoors from both LocationManager Gps and FLP. LocationManager wifi has less accuracy and does not have speed, altitude info.
Upvotes: 1