2011-07-22 9 views
2

こんにちは、私はアンドロイドプログラミングの新機能で、現在、ユーザーの位置を取得し、地図上にマーカーを配置するために位置マネージャを使用するアプリケーションを開発しています。私はAsyncTaskを使用してLocationListenerを実行しようとしており、ユーザーの場所が変更されたときにマーカーを絶えず更新しています。 これはあなたが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

私が働いているクラスです。 Click Here

実際に、それはあなたが が一緒にこれらを使用することはできませんように、2つのスレッドモデルは、互換性がありませんを意味します。 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) {} 
}