I am using the location manager's requestLocationUpdates() method to receive an intent to my broadcast receiver periodically. The system is correctly firing the intent to my receiver, and I have been able to use it correctly. The only problem is that the GPS location provider only stays active for a few seconds after the initial location acquisition, and I need it to stay on a little longer so that the location estimates are more accurate.
My question is how to make the GPS location provider stay active for each periodic request that comes from the LocationManager requestLocationUpdates. Does anyone know how to do this?
Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.
Try something like this. I think it is the right approach
private void createGpsListner()
{
gpsListener = new LocationListener(){
public void onLocationChanged(Location location)
{
curLocation = location;
// check if locations has accuracy data
if(curLocation.hasAccuracy())
{
// Accuracy is in rage of 20 meters, stop listening we have a fix
if(curLocation.getAccuracy() < 20)
{
stopGpsListner();
}
}
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
}
private void startGpsListener()
{
if(myLocationManager != null)
// hit location update in intervals of 5sec and after 10meters offset
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, gpsListener);
}
private void stopGpsListner()
{
if(myLocationManager != null)
myLocationManager.removeUpdates(gpsListener);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With