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()));
}