开发者

Android: How to keep GPS active until more accurate location is provided?

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 re开发者_开发技巧quest that comes from the LocationManager requestLocationUpdates. Does anyone know how to do this?


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 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.


There is a example about get GPS location with timeout.

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


To get GPS location periodically, get the location from onLocationChanged method of locationListener and in onResume method specify the timing in milliseconds for getting periodic updates

onResume
location_manager.requestLocationUpdates(provider, 1000, 1, MainActivity.this);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜