user2166934
user2166934

Reputation: 21

GPS shows old data

I have a activity, where request the gps_provider and if disabled then the network_provider. The problem is, when the gps sensor is enabled, but the signal not received (e.g. in a house), he will take the old data (location not null) and not the network_provider of the new position. Can I clear the old gps data?

Here the code:

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    .....

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


    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location == null) {
        if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location == null) {
            showGPSDisabledAlertToUser();
        }
    }
    if (location != null) {
        this.onLocationChanged(location);
    }


public void onLocationChanged(Location l) {
    locateLandkreis(l);
}

private void locateLandkreis(Location l) {
    new DownloadWarn(this).execute(l); 
}

private class DownloadWarn extends AsyncTask<Location, Integer, String> {

    .....

    @Override
    protected String doInBackground(Location... loc) {

        ......

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.removeUpdates(GPSActivity.this);

        return data;
    }

Thanks Oliver

Upvotes: 0

Views: 424

Answers (1)

Ridcully
Ridcully

Reputation: 23665

You should check the time of the location you get from the GPSProvider, and if it's older than a certrain threshold, also go for the NetworkProvider's location.

So, instead of

if (location == null) { ...

Do this

if (location == null || 
    System.currentTimeMillis()-location.getTime() > THRESHOLD) { ...

where THRESHOLD would be a treshold in milliseconds.

Upvotes: 1

Related Questions