views:

1314

answers:

3

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?

A: 

if you keep your LocationListener active, it should continue to receive updates to onLocationChanged() if the fix accuracy narrows. and indeed location.getAccuracy() will tell you the current accuracy

maybe set minTime and minDistance both to 0 to receive updates with greater frequency? will use more battery, but is more preise.

jspcal
I am using a broadcast receiver with the PendingIntent version of the method. Furthermore, the fix accuracy does get better, but it stops updating well before its accuracy potential, so I can't just use getAccuracy(). I need it to continue to update until the accuracy gets much better, and it's not waiting for long enough.
Doughy
I'm not using a proximity alert. I am requesting that the LocationManager give me periodic fixes that come back to my application via a broadcast receiver, which happens every time the location "changes". The LocationManager or GPS location provider do not stay active long enough for the locations to be very accurate. Even If I use a listener, that doesn't change the fact that the GPS doesn't remain active for very long.
Doughy
Have you tried jspcal's suggestion of setting the minTime to 0 or similar? If that gives you frequent updates that soon become accurate enough (or after a timeout) you could then update your LocationManager updates request back to the desired minTime value.
Christopher
+4  A: 

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);
}
zidane
A: 

There is a example about get GPS location with timeout.

http://sikazi.blogspot.com/2010/09/android-gps-timeout.html#more

Shawn