2011-09-21 44 views
0

我在运行中可靠地调节gps更新速率时遇到了困难。下面的方法似乎与我阅读的所有内容保持一致,偶尔会更新一次或两次更新率(例如,每四秒钟读取一次GPS读数),但之后它会保持一定的速率并不再改变。Android LocationManager不会如预期的那样调节

private LocationManager _locationMgr; 
private LocationListener _locationListener; 
private int _secondsPerUpdate=-1; 

// Constructor 
public AshGps(Activity l_activity, int l_secondsPerUpdate) 
{ 
    _locationMgr = (LocationManager) l_activity.getSystemService(Context.LOCATION_SERVICE); 
    _locationListener = new mylocationlistener(); 
    _locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, l_secondsPerUpdate*1000, 0, _locationListener); 
    _secondsPerUpdate = l_secondsPerUpdate; 
} 


// Called up to once every three seconds 
// to change the update rate 
void ChangeUpdateRate(int l_secondsPerUpdate) 
{ 
    if(_secondsPerUpdate != l_secondsPerUpdate) 
    { 
     _locationMgr.removeUpdates(_locationListener); 
     _locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, l_secondsPerUpdate*1000, 0, _locationListener); 
     _secondsPerUpdate = l_secondsPerUpdate; 
    } 
} 

// Methods handles the incoming GPS reading 'event' 
private class mylocationlistener implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location location) 
    { 
... 

回答

0

每次更新的时间段是对Android的建议。它并不总是被尊重。引用文档:

此字段仅用作提示以节省电源,位置更新之间的实际时间可能大于或小于此值。

相关问题