2011-07-22 82 views
2

嗨,我是新來的android編程和當前正在開發的應用程序,使用位置管理器來獲取用戶位置並在地圖上放置一個標記。我正在嘗試使用AsyncTask來運行LocationListener,並在用戶位置發生變化時不斷更新標記。 這是類的我的工作......LocationListener InSide AsyncTask

public class IncidentActivity extends MapActivity{ 


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

     mapView = (MapView)findViewById(R.id.mapView); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setTraffic(true); 


     mapController = mapView.getController(); 
     String coordinates[] = {"-26.167004","27.965505"}; 
     double lat = Double.parseDouble(coordinates[0]); 
     double lng = Double.parseDouble(coordinates[1]); 
     geoPoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6)); 
     mapController.animateTo(geoPoint); 
     mapController.setZoom(16); 
     mapView.invalidate(); 

     new MyLocationAsyncTask().execute(); 

    } 



     private class MyLocationAsyncTask extends AsyncTask<Void, Location, Void> implements LocationListener{ 
     private double latLocation; 
     private Location l; 

     //location management variables to track and maintain user location 
     protected LocationManager locationManager; 
     protected LocationListener locationListener; 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      Looper.prepare(); 
       locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener); 

       this.publishProgress(l); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

      super.onPostExecute(result); 
     } 

     @Override 
     protected void onProgressUpdate(Location... values) { 

      super.onProgressUpdate(values); 
     } 

     //this method is never executed i dont know why...? 
     public void onLocationChanged(Location location) { 
      if (location != null){ 
       latLocation = location.getLatitude(); 
       Toast.makeText(getBaseContext(), " Your latLocation :" + latLocation, Toast.LENGTH_LONG).show(); 
       //Log.d("Your Location", ""+latLocation); 
      } 
     } 

     public void onProviderDisabled(String provider) { 


     } 

     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 


     } 


} 
+2

那麼,什麼是你的問題? – Rasel

+0

onLocationChanged方法從不調用。 –

回答

1

從我已閱讀並嘗試了,你不能使用活套(這是由LocationListener的需要),在裏面的AsyncTask。 Click Here

其實它指的是兩種線程模型是不兼容的,所以你不能 使用這些結合在一起。 Looper希望擁有與 相關聯的線程,而AsyncTask擁有它創建的線程,供您在 背景中運行。因此它們相互衝突,不能一起使用。

Dianne Hackborn建議使用HandlerThread,但我成功地讓我的IntentService工作。我承認我的代碼還是有點破解的。

2

我剛剛實施,例如AsyncTask

class GetPositionTask extends AsyncTask<Void, Void, Location> implements LocationListener 
{ 
    final long TWO_MINUTES = 2*60*1000; 
    private Location location; 
    private LocationManager lm; 

    protected void onPreExecute() 
    { 
     // Configure location manager - I'm using just the network provider in this example 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); 
     nearProgress.setVisibility(View.VISIBLE); 
    } 

    protected Location doInBackground(Void... params) 
    { 
     // Try to use the last known position 
     Location lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     // If it's too old, get a new one by location manager 
     if (System.currentTimeMillis() - lastLocation.getTime() > TWO_MINUTES) 
     { 
      while (location == null) 
       try { Thread.sleep(100); } catch (Exception ex) {} 

      return location; 
     } 

     return lastLocation; 
    } 

    protected void onPostExecute(Location location) 
    { 
     nearProgress.setVisibility(View.GONE); 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.removeUpdates(this); 

     // HERE USE THE LOCATION 
    } 

    @Override 
    public void onLocationChanged(Location newLocation) 
    { 
     location = newLocation; 
    } 

    public void onProviderDisabled(String provider) {} 
    public void onProviderEnabled(String provider) {} 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}