问题描述:

I am trying to make a method that sends the users addresss in a text message. (I know how to get the address, and how to send a text message already)

I have created a LocationHelper class that I use to get latitude and longitude and then return an address as a String.

The issue is that I don't want the sending message code to execute until the address is sent back. How can I delay my code to wait until the address is returned.

 public void sendMessage(){

String address = // where I call location helper to get address

// Message is sent using the address string - code below executes before the address is returned - need to delay running the code below

sending message functionality goes here

...

}

I will also want to other methods for example sendEmail(), that will also call the location helper to get the address and then send an email with the address.

This is why I need to be able to wait until an address is returned before running the sending code.

Thank you.

网友答案:

What you should do is call a LocationListener, and onLocationChanged send the message. I think this is easier than using a Location Helper. This would look like:

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 70, locationListener);
locationListener = new LocationListener() 
    public void onLocationChanged(Location location) {
    longitude = location.getLongitude();
    latitude = location.getLatitude();
        Geocoder gCoder = new Geocoder(myContext);
        ArrayList<Address> addresses = gCoder.getFromLocation(latitude, longitude, 1);
        if (addresses != null && addresses.size() > 0) {
            // get the address
        }
        // send the message
相关阅读:
Top