2012-07-21 61 views
1

我正在尝试创建一个摘要AsyncTask,以制作一个加载程序并提供一些额外的功能。创建摘要Asynctask

public abstract class AbstractLoader <Params, Progress, Result> extends AsyncTask <Params, Progress, Result> { 
private boolean ready; 
protected Context context; 
protected Application application; 

public AbstractLoader (Context context, Application application) { 
    this.context = context; 
    this.application = application; 
} 

public boolean isReady() { 
    return ready; 
} 

protected void setReady(boolean ready) { 
    this.ready = ready; 
} 

我已经创造了很多子类它,他们部分工作,doInBackground(params)执行,但onPreExecute()不执行。唯一被执行的方法是抽象类。

这里是子类:

public class GPSLoader extends AbstractLoader<Void, Void, Integer> { 

private Location location; 
private LocationRestaurante[] locations; 
private float[] distancias; 
private float distanciaMinima; 

private int idCentroComercial; 

final String TAG = getClass().getCanonicalName(); 

public GPSLoader(Context context, Application application) { 
    super(context, application); 
    distanciaMinima = 100; 

    LocationManager locationManager = (LocationManager) context 
      .getSystemService(Context.LOCATION_SERVICE); 

    QuickOrderLocationListener locationListener = new QuickOrderLocationListener(
      this); 

    try { 
     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
    } catch (Exception e) { 
     Log.d(TAG, e.getMessage()); 
    } 
    Log.d(TAG, locationManager.toString()); 
} 

@Override 
protected void onPreExecute() { 
    Log.d (TAG, "onPreExecute"); 
} 

@Override 
protected Integer doInBackground(Void... params) { 
    synchronized (this) { 
     while (location == null) { 
      try { 
       wait(20); 
      } catch (InterruptedException e) { 
       Log.d(TAG, e.toString()); 
      } 
     } 
    } 

    locations = obtenerLocationsFijas(); 

    for (int i = 0; i < locations.length; i++) { 
     float distancia = calcularPosicion(location, locations[i])[0]; 
     if (distancia < 100) { 
      return new Integer(locations[i].getId()); 
     } 
    } 
    Log.d(TAG, "termina el gps loader"); 
    return null; 
} 

@Override 
protected void onPostExecute(Integer result) { 
    setReady(true); 
} 

public int getIdCentroComercial() { 
    return idCentroComercial; 
} 

private float[] calcularPosicion(Location loc, Location baseLoc) { 
    float[] results = null; 
    Location.distanceBetween(loc.getLatitude(), loc.getLongitude(), 
      baseLoc.getLatitude(), baseLoc.getLongitude(), results); 
    return results; 
} 

private LocationRestaurante[] obtenerLocationsFijas() { 
    return null; 
} 

private class QuickOrderLocationListener implements LocationListener { 

    private GPSLoader gpsLoader; 

    public QuickOrderLocationListener(GPSLoader gpsLoader) { 
     this.gpsLoader = gpsLoader; 
    } 

    public void onLocationChanged(Location location) { 
     gpsLoader.location = location; 
    } 

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

    public void onProviderEnabled(String provider) { 
    } 

    public void onProviderDisabled(String provider) { 
    } 
} 

private class LocationRestaurante extends Location { 
    private int id; 

    public LocationRestaurante(String s) { 
     super(s); 
    } 

    public int getId() { 
     return id; 
    } 

} 

} 

我在这里调用方法:

private synchronized int getPosition() { 
    gpsLoader.doInBackground(); 
    while (gpsLoader.isReady() == false) { 
     try { 
      wait(20); 
     } catch (InterruptedException e) { 
      Log.d(TAG, e.toString()); 
     } 
    } 

    int idCentroComercial = gpsLoader.getIdCentroComercial(); 
    return idCentroComercial; 
} 

onPostExecute没有任何执行。 谢谢

回答

0

gpsLoader.doInBackground();更改为gpsLoader.execute(null)作为doInBackground()不应该手动调用。