2010-11-10 66 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); 
} 

任何帮助,将不胜感激。

回答