Frage

I have a class implementing the LocationListener

public class GetLocation implements LocationListener {

@Override
public void onLocationChanged(Location location) {
    Log.i("GetLocation", "Location changed: " + location.getLatitude()
            + ", " + location.getLongitude());

}

In my activity,

final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    final GetLocation locationListener = new GetLocation();
locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0, locationListener);

     .....  //Check whether the location is changed

    locationManager.removeUpdates(locationListener);

    updateGPStoServer();

After onLocationChanged() is called once, I want to do a upload task and cancel the listener, so I am asking how can I wait for the onLocationChanged() then do my tasks?

War es hilfreich?

Lösung

After onLocationChanged() is called once, I want to do a upload task and cancel the listener, so I am asking how can I wait for the onLocationChanged() then do my tasks?

I think you need to move the call to updateGPStoServer() into your Listener.onLocationChanged method; that would implement the "waiting" that you're looking for. (On a separate note: updateGPStoServer() should be implemented to create a background Thread to do the updates to the server. But you knew that, right? :-)

Also, it sounds like you really want to be calling LocationManager.requestSingleUpdate instead of LocationManager.requestLocationUpdates. That would remove the need to call locationManager.removeUpdates(locationListener).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top