0

我有以下位置列表器来获取用户的当前位置。我使用网络和 GPS 等了大约 45 秒才放弃。问题是有时(大约 15% 的时间)即使在 45 秒后我也会得到一个空位置。我没有发现发生这种情况的模式。任何人都可以阐明可能发生的事情吗?

public class MyLocationListener implements LocationListener {

private static final float DELTA_ACURACIDADE = 30;

public static final long DEFAULT_TIME_UPDATES = 0;

public static final float DEFAULT_DISTANCE_UPDATES = 0;

private static final long TIME_CONSIDERED_OLD_LOCATION = 1000 * 150;

private Location location;

@Override
public void onLocationChanged(Location newLocation) {

    if (newLocation != null && newLocation.getLatitude() != 0.0d && newLocation.getLongitude() != 0.0d) {

        boolean isOk = false;

        if (this.location == null) {
            this.location = newLocation;
        } else {

            long deltaTime = newLocation.getTime() - this.location.getTime();

            boolean isNewer = deltaTempo > 0;

            boolean isAlotNewer = deltaTempo > TIME_CONSIDERED_OLD_LOCATION;

            int deltaAcuracidade = (int) (this.location.getAccuracy() - newLocation.getAccuracy());
            boolean isEqualOrMoreAccurate = deltaAcuracidade >= DELTA_ACURACIDADE;

            if ((isNewer && isEqualOrMoreAccurate) || isAlotNewer) {
                aceitarNovaLocation = true;
            }

            if (aceitarNovaLocation) {
                this.location = newLocation;

            }
        }
    }
}

public Location getLocation() {
    return this.location;
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

}

}





public class checkInActivity extends Activity {

private long WAIT_FOR_LOCATION = 1000 * 40;

private Location roteiroLocation;

private MyLocationListener locationListner;

private LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    this.locationListner = new MyLocationListener();

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

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MyLocationListener.DEFAULT_TIME_UPDATES,
            MyLocationListener.DEFAULT_DISTANCE_UPDATES, this.locationListner);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            MyLocationListener.DEFAULT_TIME_UPDATES, MyLocationListener.DEFAULT_DISTANCE_UPDATES,
            this.locationListner);

}

protected void processLocation() {

    new AsyncTask<Void, Void, Void>() {

        private boolean wasLocationAccepted = false;

        private ProgressDialog dialog = new ProgressDialog(checkInActivity.this);

        @Override
        protected void onPreExecute() {

            this.dialog.setMessage("message");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            Long t = Calendar.getInstance().getTimeInMillis();

            Location location = null;

            while (Calendar.getInstance().getTimeInMillis() - t < WAIT_FOR_LOCATION) {

                location = locationListner.getLocation();

                if (location != null && location.getAccuracy() < MIN_ACCURACY_LOCATION
                        && location.distanceTo(place) < MIN_ACCURACY_LOCATION) {
                    wasLocationAccepted = true;
                    break;
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            locationManager.removeUpdates(locationListner);

            if (this.dialog.isShowing(){
                this.dialog.dismiss();
            }

            SigoMobileHelper.performOnBackgroundThread(runnable);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "AsyncTask.onPostExecute");

            finish();
        }

    }.execute();

}

@Override
protected void onStart() {
    processLocation();
    super.onStart();
}

}
4

1 回答 1

1

你的代码没问题。

您无法获取位置,因为 GPS 信号可能较低或卫星没有应答,并且应用程序无法下载位置。

于 2013-07-26T14:05:31.920 回答