Android LocationListner无法找到我的位置

时间:2013-07-26 14:02:27

标签: android

我有以下位置列表器来获取用户当前位置。我等待大约45秒才放弃,使用网络和GPS。问题是,有时(大约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();
}

}

1 个答案:

答案 0 :(得分:1)

您的代码没问题。

你没有获得位置,因为GPS信号可能很低或卫星没有应答,而且app无法下载位置。