2014-01-05 49 views
0

我试图做一个方法,发送用户地址在短信中。 (我知道如何获取地址,以及如何发送短信)Android等待位置侦听器继续执行代码

我创建了一个LocationHelper类,用于获取纬度和经度,然后以String形式返回地址。

问题是我不希望发送消息代码执行,直到地址被发回。我如何推迟我的代码等待地址返回。

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 
    ... 
    } 

我也会想其他方法,例如sendEmail(),这也将拨打的帮手来获取地址,然后用地址发送电子邮件。 这就是为什么我需要在运行发送代码之前能够等到返回地址的原因。

谢谢。

回答

0

你应该做的是打电话LocationListeneronLocationChanged发送消息。我认为这比使用位置助手更容易。这看起来像:

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 
+0

嗨,我所要做的是使用地址的各种方法,例如使用地址发短信,发送电子邮件,打开谷歌地图等 这就是为什么我使用LocationHelper,以便我可以从每种方法调用它,等待该值,然后根据它的方法执行操作。 – user3013243