How to avoid the battery drain while implementing the GPS?
In my app I'm using a GPS listener. I need to turn off the GPS and activate it later. Currently I'm using
locationManager.removeUpdates(locationListener);
to turn off the gps. That will turn the GPS off. But later I'm not able to开发者_如何学C turn it on? Is there any other methods to turn off the gps?
For activating the GPS I'm using
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updatesInterval, 0, locationListener);
You can make a service which implement location Listener so when ever you want to start location listener then you can start service and put
ocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updatesInterval, 0, locationListener);
line in onCreate() method
and when you want to close location update the stop service and put line
locationManager.removeUpdates(locationListener);
to onDistroy() method
I want the gps to return the most accurate value. So I set the minValue and minDistance value of the requestLocationUpdates function to 0.
Then to solve the problem with battery drain I'll start and stop the gps (which is a service) when the app comes to foreground and when the app enters background.
To start the GPS i used
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
in the onCreate of the service.
To end the GPS i used
locationManager.removeUpdates(locationListener);
in the onDestroy of the service.
You have to do the starting and stopping of the gps in a service, so that you can start and stop the same instances of the location listener otherwise this thing will go drastically wrong
精彩评论