2013-04-23 91 views
1

我需要你的幫助,關於android中的位置更新。以下是我的代碼獲取位置更新,它工作正常。但是當我在主類的oncreate方法中獲取存儲的變量時,它返回無效的消息體。經過深入研究,似乎我在oncreate方法中調用的變量是空的。你能告訴我如何獲取地址,因爲它出現在onlocationChanged方法中。謝謝!從位置監聽器調用和獲取oncreate方法的位置android

調用類onCreate方法:

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      listener = new Mylocation(); 

      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
      String address1= listener.getAddress(); 

      { 
       sendSms(phone, message, false); 
      } catch (Exception e) { 

       Log.i("Error Is ", e.toString()); 
      } 

     } 

位置類:

class Mylocation implements LocationListener{ 

    double lat, lon; 
    static final String address=""; 

    public void onLocationChanged(Location location) 
    { 
     //... 

     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
     address = GetAddressDetail(lat, lon); 
     Log.i("Messge is", address); //working here 
     } 

    public String getAddress(){ //not returning the address 
     return address; 
    } 

public String GetAddressDetail(Double lat2, Double lon2) 
    { 
     Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1); 
      if(addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder("Address:"); 
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)); 
       } 
       ret = strReturnedAddress.toString(); 
      } 
      else{ 
       ret = "No Address returned!"; 
      } 
} 
     return ret; 
} 
} 
+1

您的地址變量是最終的,這意味着它只能分配一次。因此,將其重新分配給GetAdressDetail結果將不起作用。 – 2013-04-23 12:03:49

+0

它沒有出現,你已經爲'message'聲明瞭一個值,並且你沒有顯示它的實例化。 – jnthnjns 2013-04-23 12:07:19

+0

@LeonLucardie,雖然地址被宣佈爲最終它仍然在那部分工作。唯一的是我不能通過getAddress在Android的 – 2013-04-23 12:10:31

回答

1

要調用getAdress後直接您設置的LocationManager探測的位置。當你以這種方式調用getAddress時,onLocationChanged方法可能還沒有被調用。我建議將它改爲如下所示,以確保它已被調用:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listener = new Mylocation(); 

     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,new LocationListener(){ 
      @Override  
      public void onLocationChanged(Location location) { 
      //... 

       long lat = location.getLatitude(); 
       long lon = location.getLongitude(); 
       String address = GetAddressDetail(lat, lon); 
       //Do whatever you want to do with the address here, maybe add them to the message or something like that. 
       sendSms(phone, message, false); 
      } 
     }); 

} 

public String GetAddressDetail(Double lat2, Double lon2) 
{ 
     Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1); 
      if(addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder("Address:"); 
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)); 
       } 
       ret = strReturnedAddress.toString(); 
      } 
      else{ 
       ret = "No Address returned!"; 
      } 
     } 
     return ret; 
} 
+0

我會盡力讓你知道。謝謝您的回答。 – 2013-04-23 14:04:27

+0

我已經嘗試了同樣的事情,但您認識到的主要問題是偵聽器中的sendSMS。它的工作,並感謝你耐心和時間。順便說一句,你認爲這是發送位置到另一個電話的正確方法。 – 2013-04-23 14:16:31

2

確保您的變量正確初始化。我在這個問題中沒有看到這個證據,所以我只是在檢查。

// Instantiation 
Mylocation listener; 
String phone; 
String message; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Initialization 
    listener = new Mylocation(this); 
    phone = ""; 
    message = ""; 

    // These two lines aren't really necessary, 
    // this should be in your MyLocation class  
    //locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 

    // Add this line, we are going to initialize this class 
    // and make sure that address gets set 
    listener = new Mylocation(); 
    String address1 = listener.getAddress(); 

    try { 
     // If neither phone nor message is empty lets sendSms() 
     if (!phone.isEmpty() || !message.isEmpty()) { 
      sendSms(phone, message, false); 
     } 
    } catch (Exception e) { 
     Log.i("Error Is ", e.toString()); 
    } 

} 

更改String address給私人,並在吸氣嘗試return this.address;

class Mylocation implements LocationListener { 

    double lat, lon; 
    // Let's make this private so it can't be accessed directly, 
    // since you have the setter and getter. 
    private String address = ""; 

    // Make sure you are overriding this method 
    @Override  
    public void onLocationChanged(Location location) { 
     /** ... */ 
     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
     address = GetAddressDetail(lat, lon); 
     Log.i("Messge is", address); 
    } 

    public String getAddress(){ 
     return (address.isEmpty()) ? "Address not set" : this.address; 
    } 

    public String GetAddressDetail(Double lat2, Double lon2) { 
     Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH); 
     try { 
      List<Address> addresses = geocoder.getFromLocation(lat2,lon2, 1); 
      if(addresses != null) { 
       Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder("Address:"); 
       for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress.append(returnedAddress.getAddressLine(i)); 
       } 
       ret = strReturnedAddress.toString(); 
      } 
      else{ 
       ret = "No Address returned!"; 
      } 
     } 
     return ret; 
    } 
} 

編輯

我更改了代碼,我的回答以上,檢查評論。我還打算建議您MyLocation類的其他方法:

class Mylocation implements LocationListener { 
    protected LocationManager locationManager; 
    private Context activityContext; 

    // The initializing method, this fires off first 
    // when a new instance of the class is created 
    public MyLocation(Context context) { 
     this.activityContext = context; 
     locationManager = (LocationManager) activityContext.getSystemService(LOCATION_SERVICE); 
     if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);) { 
      locationManager.requestLocationUpdates(
       NETWORK_PROVIDER, 
       MIN_TIME, 
       MIN_DISTANCE, 
       this 
      ); 
     } 

     getLocation(); 
    } 

    private double lat; 
    private double lon; 

    public double getLat() { 
     return this.lat; 
    } 

    public double getLng() { 
     return this.lng; 
    } 

    public void getLocation() { 
     if (location == null) { 
      locationManager.requestLocationUpdates(NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); 
      if (locationManager != null) { 
       location = locationManager.getLastKnownLocation(NETWORK_PROVIDER); 
       if (location != null) { 
        // Set the coordinate variables 
        lat = location.getLatitude(); 
        lon = location.getLongitude(); 
        Log.i("Network", "Lat: " + latitude + "/Lng: " + longitude); 
       } 
      } 
     } 
    } 
} 
+0

感謝您分享編碼。那麼,它正在使用預定義的字符串作爲我發送的短信的變量消息。唯一的事情是我想從後續編碼中得到來自onLocationChanged()的位置信息。 – 2013-04-23 13:18:10

+0

監聽器沒有問題,但唯一的問題是從中獲取地址,並將它作爲消息值創建和發送。 – 2013-04-23 13:19:36

+0

我做了一些更改以幫助您。詳情請參閱上面的編輯。 – jnthnjns 2013-04-23 14:06:57