我正在尝试创建一个 abstract 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
也不执行。谢谢