AsyncTask的doInBackground阻止了locationManager

时间:2016-03-16 12:34:08

标签: android android-asynctask location

我想找到用户的位置仅一次,并显示ProgressDialog(PD),直到该位置未知。

我正在使用AsyncTask,在显示PD的onPreExecute()中,调用了locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);。由于使用GPS查找位置可能需要很长时间,因此在doInBackground()我通过调用listener.isLocationAvailable()检查位置是否可用。

for(int i = 0; i < 100; i++) {...}循环替换while循环后,输出是下一个:

I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling
I/System.out: cycling

100次......

I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333
I/System.out: 65.9667, -18.5333

(调用onLocationChanged(位置位置)后,在100x循环后显示100倍)

这是AsyncTask:

 private class PositioningTask extends AsyncTask<Void, Void, String> {
    Context context = TransportActivity.this;
    ProgressDialog progressDialog;

    TransportLocationListener listener;
    LocationManager locationManager;
    private boolean gpsEnabled;
    private boolean networkEnabled;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(TransportActivity.this);
        progressDialog.setTitle("Loading position, please wait");
        progressDialog.setMessage(getString(R.string.please_wait));
        progressDialog.setCancelable(false);
        progressDialog.show();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listener = new TransportLocationListener();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        }
    }

    @Override
    protected String doInBackground(Void... params) {
        String position = "";
        try{
            while (true){
                if (listener.isLocationAvailable()) {
                    Location location = listener.getLocation();
                    position = location.getLatitude() + "," + location.getLongitude();

                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                            || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        locationManager.removeUpdates(listener);
                    }
                    System.out.println("POSITION: " + position);
                    break;
                }
                System.out.println("cycling");
                Thread.sleep(1000);
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return position;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
    }
}

和TransportLocationListener:

public class TransportLocationListener implements LocationListener {
private Location location = null;
private Boolean available = false;

public boolean isLocationAvailable() {
    return  available;
}

public Location getLocation() {
    return location;
}

@Override
public void onLocationChanged(Location location) {
    this.location = location;

    available = true;

    System.out.println(location.getLatitude() + ", " + location.getLongitude());
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

1 个答案:

答案 0 :(得分:0)

  

AsyncTask的doInBackground阻止了locationManager

我认为您的问题是您获得了不断的位置更新流?

您对此行的请求:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

是接收位置更新,minTime等于0,minDistance等于0.这意味着&#39; onLocationChanged&#39;即使没有完成位置更改,也会被调用。将此值更改为1000和0.1f,以便使用实际新值更少地调用onLocationChanged

相关问题