1

I have an android application which will track location from background.i am using fused location api with following configuration

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

I need location updates only if device moves.Sometimes everything works perfectly.But sometimes i receive location updates even if device in stationary.

Why this issue happens? If i set displacement,do i need to set interval and fastest interval? Is this issue related to google play service version?

3 Answers3

0

It will update your location every 60 seconds if your mobile moves or not beaus of your give interval duration.

Bhavin Sorathiya
  • 141
  • 1
  • 12
0

Request your location updates using a GoogleApiClient e.g. like this and use Toasts to verify your implementation:

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    float LOCATION_REFRESH_DISTANCE = 5000;
    long LOCATION_REFRESH_TIME = 0;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mlocationListener);
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if(mLastLocation!=null){
        Toast.makeText(getApplicationContext(), "YES! mLastLocation!=null", Toast.LENGTH_SHORT).show();
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();           
        Toast.makeText(getApplicationContext(), "Latitude = " + latitude + "\nLongitude = " + longitude, Toast.LENGTH_SHORT).show();
    }
}

Here you would be able to parametrize your Location requests with the variables LOCATION_REFRESH_DISTANCE and LOCATION_REFRESH_TIME (in milliseconds)

You would need of course the LocationListener object mLocationListener that your GoogleApiClient mGoogleApiClient needs to get the last known / current location and a Location object (I called it mLastLocation) where to store it. eg.:

private Location mLastLocation;
LocationListener mlocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
    mLastLocation=location;

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};
0

What you likely need is the PendingIntent version of FusedLocationProviderApi. This method is suited for the background use cases, more specifically for receiving location updates, even when the app has been killed by the system.

2cupsOfTech
  • 5,953
  • 4
  • 34
  • 57