2010-11-10 83 views
1

我正在使用以下代碼來獲取GPS位置。 現在我想要獲得GPS位置而不需要自動更新。如何獲得GPS位置一次,而沒有自動更新?

例如,我想獲取最新的位置,只有按鈕點擊一次並非所有的時間。

所以請告訴我應該使用minTime和Distance值什麼值,同時獲取位置更新一次,沒有間隔和自動更新?

我應該在該按鈕而不是onResume中調用位置更新嗎?

onbutton click() { 
    mlocManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    bestProvider = mlocManager.getBestProvider(criteria, false); 
    loc = mlocManager.getLastKnownLocation(bestProvider); 

    if(loc != null) { 
    MyDeviceLatitude = loc.getLatitude(); 
    MyDeviceLongitude = loc.getLongitude(); 
    } else { 
    showError(); 
    }  
} 

@Override 
public void onLocationChanged(Location argLocation) { 
    if (argLocation != null) { 
    loc = argLocation; 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    mlocManager.removeUpdates(this); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    Criteria criteria = new Criteria(); 
    mlocManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    bestProvider = mlocManager.getBestProvider(criteria, false); 
    mlocManager.requestLocationUpdates(bestProvider, 20000,1 , this); 
} 

任何幫助,將不勝感激。

回答