Android:如何在提供更准确的位置之前保持GPS处于活动状态?

Dou*_*ghy 6 gps android geolocation

我正在使用位置管理器的requestLocationUpdates()方法定期向广播接收器接收意图.系统正确地向我的接收器发出意图,我已经能够正确使用它.唯一的问题是GPS位置提供商在初始位置获取后仅保持活动状态几秒钟,我需要它保持更长时间,以便位置估计更准确.

我的问题是如何使GPS位置提供程序对来自LocationManager requestLocationUpdates的每个定期请求保持活动状态.有谁知道如何做到这一点?

zid*_*ane 9

尝试这样的事情.我认为这是正确的方法

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);
}
Run Code Online (Sandbox Code Playgroud)