androidgpslocationlocation-providerfusedlocationproviderapi

Android Location API not properly tracking the user


I have a background service which should track the user movement with his car, and send the data to my server. I have two variables for sending the location, either 60 seconds have passed or the user has moved 100 meters.

On my service here is how I start listening to locations:

mLocationRequest = LocationRequest.create();
mLocationRequest.setSmallestDisplacement(100);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(60 * 1000);

mLocationClient = new LocationClient(context, connectionCallbacks, onConnectionFailedListener);
mLocationClient.connect();

and the listeners:

ConnectionCallbacks connectionCallbacks = new ConnectionCallbacks() {

    @Override
    public void onConnected(Bundle arg0) {

        mLocationClient.requestLocationUpdates(mLocationRequest, locationListenerAPI);

        //process the last location, if available
        Location loc = mLocationClient.getLastLocation();
        if (loc != null) {
            prepareLocation(loc);
        }
    }

    @Override
    public void onDisconnected() {
    }
};

LocationListener locationListenerAPI= new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        //A location has been read, process it
        prepareLocation(location);
    }

};

So, while the service is running and I driver around with the car, onLocationChanged only fires a few times, it does not take into consideration either the time or meters to fire a location read. I have data connection and the GPS icon is visible on notification bar the whole time the service is running.

Any ideas why is not working ?


Solution

  • In the end, I just did it like this:

    I've setInterval(5000) and setFastestInterval(5000) and then, for each read location, if distance between them is bigger than 100m, I send the location to my server. I gave up on using smallestDisplacement. This seem to work reliably.