dev90
dev90

Reputation: 7539

onLocationChanged gets called multiple times on start of application

I have written following service,The idea is to send User location to server on application launch and when user travels more than 500m, The problem is that on launch it calls onLocationChanged 3 times.

I am unable to understand that from where its calling it 3 times. Kindly guide me how to resolve this problem.

public class LocationUpdateService extends Service implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {



    private static final long BACKGROUND_INTERVAL = 60000;

    final private String TAG = LocationUpdateService.class.getSimpleName();
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    private String LOCATION_PREF = "LOCATION_PREF";



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setFastestInterval(30000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        mLocationRequest.setSmallestDisplacement(500);
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i("Location Service", "onLocationChanged: " + location.toString());
        saveLocationandReport(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
    }

Upvotes: 1

Views: 2825

Answers (2)

Haris Qurashi
Haris Qurashi

Reputation: 2124

You cannot control onLocationChanged, it is manage by location service. In your case you can set a buffer/wait time, if you have location update within buffer time you can check its compare its accuracy with previous location update which ever is best use it.

private int FASTEST_INTERVAL = 1000; // use whatever suits you
private Location currentLocation = null;
private long locationUpdatedAt = Long.MIN_VALUE;

@Override
public void onLocationChanged(Location location) {
    boolean updateLocationandReport = false;
    if(currentLocation == null){
        currentLocation = location;
        locationUpdatedAt = System.currentTimeMillis();
        updateLocationandReport = true;
    } else {
       long secondsElapsed = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - locationUpdatedAt);
       if (secondsElapsed >= TimeUnit.MILLISECONDS.toSeconds(FASTEST_INTERVAL)){
            // check location accuracy here
            currentLocation = location;
            locationUpdatedAt = System.currentTimeMillis();
            updateLocationandReport = true;
       }        
    }
    if(updateLocationandReport){
        //  send your location to server
    }
}

Upvotes: 4

Mick
Mick

Reputation: 25481

You don't control when the onLocationChanged is called - it is the location service which calls it.

It may call if for a number of reasons - the most obvious is when the users actually moves and it detects this and sends you an update, but it also may be called when the location service gets a more accurate location.

As a general rule it takes a little time for an accurate location so if you need accuracy it may make sense to allow for this in your application.

Upvotes: 2

Related Questions