简体   繁体   中英

how to get GPS location once and without automatic updates?

i am using following code to get GPS location. now i want to get GPS location without automatic updates.

for example i want to get latest location on button click only once not all the time.

so please tell what value should i use for minTime and Distancewhile getting location updates for once and without intervals and automatic updates?

and should i call location updates in that button instead of 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); 
}

any help would be appreciated.

look at this post here: Android: getLastKnownLocation out-of-date - how to force location refresh?

basically just do the stuff in "onbutton click()" to get the last position. but know that this position is the from the last time gps was active! so it might be out of date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM