2012-11-25 21 views
0

所以我正在製作一個應用程序以獲取當前位置。下面的代碼工作正常爲搜索地址添加AsyncTask

 
String stringAddress = ""; 

public void getLocation(View view){ 
    final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

    Criteria kriteria = new Criteria(); 
    kriteria.setAccuracy(Criteria.ACCURACY_FINE); 
    kriteria.setAltitudeRequired(false); 
    kriteria.setBearingRequired(false); 
    kriteria.setCostAllowed(true); 
    kriteria.setPowerRequirement(Criteria.POWER_LOW); 
    final String provider = lm.getBestProvider(kriteria, true); 

    final Location lokasi = lm.getLastKnownLocation(provider); 
    updateWithNewLocation(lokasi); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, ll); 

    edittext_position.setText(stringAddress); 
} 

private void updateWithNewLocation(Location 
    if(lokasi != null){ 
     double lat = lokasi.getLatitude(); 
     double lng = lokasi.getLongitude(); 
     Geocoder gc = new Geocoder(this, Locale.getDefault()); 

     try{ 
      List addresses = gc.getFromLocation(lat, lng, 1); 
      StringBuilder sb = new StringBuilder(); 

      if(addresses.size()>0){ 
       Address address = addresses.get(0); 
       sb.append(address.getAddressLine(0)); 
       stringAddress= sb.toString(); 
      } 

     } catch (Exception e){ 

     } 
    } 
} 

private final LocationListener ll = new LocationListener() { 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    public void onProviderEnabled(String provider) { 

    } 

    public void onProviderDisabled(String provider) { 
     updateWithNewLocation(null); 
    } 

    public void onLocationChanged(Location location) { 
     updateWithNewLocation(location); 
    } 
} 

但問題是,每次我打電話的getLocation()函數,應用第二返回結果之前掛的一對夫婦。我知道使用一個SyncTask解決這個問題,但我不知道如何開始。感謝你的幫助。

謝謝

回答

1

這是從我的應用程序的代碼片段:

public void getCurrentLocation(final ListenerGetCurrentLocation listenerGetCurrentLocation) { 
    new AsyncTask<Void, Void, List<Address>>() { 

     @Override 
     protected List<Address> doInBackground(Void... voids) { 
      Geocoder geo = new Geocoder(instance); 

      List<Address> listAddresses = null; 

      Criteria criteria = new Criteria(); 
      String bestProvider = locationManager.getBestProvider(criteria, true); 

      if (bestProvider == null) { 
       bestProvider = LocationManager.NETWORK_PROVIDER; 
      } 

      Location location = locationManager.getLastKnownLocation(bestProvider); 

      try { 
       if (location != null) { 
        listAddresses = geo.getFromLocation(location.getLatitude(), 
                 location.getLongitude(), 
                 1); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return listAddresses; 
     } 

     public void onPostExecute(List<Address> listAddresses) {     
      Address _address = null; 
      if ((listAddresses != null) && (listAddresses.size() > 0)) { 
       _address = listAddresses.get(0); 

       GeoPoint currentPosition = new GeoPoint(((int)(_address.getLatitude() * 1E6)), 
                 ((int)(_address.getLongitude() * 1E6))); 
      } 

     } 

    }.execute(); 
} 
+0

嗨,很抱歉我遲到的迴應。我現在試試你的解決方案。謝謝 – Bobby

+0

謝謝你的幫助。有用。 – Bobby

+0

歡迎您:) – Vladimir

0

requestLocationUpdates()是異步的,它不會阻止你的應用程序。它在後臺輪詢位置,然後調用監聽器。

但是,gc.getFromLocation()不是。這可能是你的滯後的原因

創建一個新的AsyncTask,Eclipse會建議你重寫那些方法

@Override 
protected List<String> doInBackground(String... params) { 
    // this is done in the background so it won't block the UI. The return type must be set to List<String> (I think default is just String) 
    return gc.getFromLocation() 
} 


@Override 
protected void onPostExecute(List<String> adresses) { 
    // called when the GC work is finished. You can now use your list of addresses and display them 
} 

不要忘記調用​​在執行任務上。

+0

對我遲到的回覆感到抱歉。我會嘗試你的解決方案 – Bobby