Archie.bpgc
Archie.bpgc

Reputation: 24012

onLocationChanged never gets called, even after requesting for updates

Start Service:

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
        PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(), 30*1000, pi);

Service:

public class LocationService extends Service implements LocationListener {

    public static double curLat = 0.0;
    public static double curLng = 0.0;
    private LocationManager mgr;
    private String best;
    private Location location;

    @Override
    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        best = LocationManager.NETWORK_PROVIDER;
        location = mgr.getLastKnownLocation(best);
        if (location != null) {

            dumpLocation(location);
            mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    5 * 1000, 0, this);
        }
        return START_NOT_STICKY;
        }
    }

    private void dumpLocation(Location l) {

        SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
                Locale.ENGLISH);
        String format = s.format(l.getTime());
        //The above time is always 21/03/2013:09:53:41 which is more than a week old
        curLat = l.getLatitude();
        curLng = l.getLongitude();
    }

    @Override
    public void onLocationChanged(Location location) {

        //This never seems to be called
        dumpLocation(location);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

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

    }
}

Every 30 secs the service is being called, but the requestLocationUpdates never seems to call onLocationChanged. And when I checked the time of the lastKnownLocation its a week old.

Applications like foursquare, local, maps etc gives the current location with GPS disabled.

What am I missing?

Upvotes: 0

Views: 304

Answers (1)

Pratik
Pratik

Reputation: 30855

I think you need to move from somewhere with your device because onLocationChange only called if the location was change. And the network location was approximate near by 1000-2000 meter so need to check on street where the location was change and getting the new location. If you used gps then it will giving you accurate but it also nearby 20-30 meter after fixed all it.

Upvotes: 1

Related Questions