85

I am writing my own background location updates for interval of every 5 minutes in android. I would like to know the difference between setInterval and setFastestInterval

When I setInterval to 5 mins and setFastestInterval to 2 mins. The location update is called every 2 mins.

I would like to know the difference. I couldn't understand what exactly is written in the developer page for this https://developer.android.com/reference/com/google/android/gms/location/LocationRequest.html

Also: Is there an inbuilt function to check the location updates only if the distances of the first update are more than 20meters with the second update?

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144

4 Answers4

132

Based on the relevant Android documentation:

  • setInterval(long) means - set the interval in which you want to get locations.
  • setFastestInterval(long) means - if a location is available sooner you can get it (i.e. another app is using the location services).

For example, you start your application and register it via setInterval(60*1000), that means that you'll get updates every 60 seconds.

Now you call setFastestInterval(10*1000). If you are the only app which use the location services you will continue to receive updates approximately every 60 seconds. If another app is using the location services with a higher rate of updates, you will get more location updates (but no more frequently that every 10 seconds).

I believe that it has a good impact on battery life consumed by your app, you define the maximum amount of time that you can wait while saying that if an update is available, you want it. The battery consumption will be credited to the app which requested the more frequent updates and not yours.

stkent
  • 19,772
  • 14
  • 85
  • 111
Ohad Zadok
  • 3,452
  • 1
  • 22
  • 26
  • 4
    Thanks for pointing out battery consumption will be credited to app that requests it more frequently. – Amit Kumar Jan 13 '16 at 17:52
  • 3
    as pointed in other comments, you can also set option to get location updates if minimum distance criteria is met. Use ```mLocationRequest.setSmallestDisplacement(10.0)``` which means get location update only if displacement is ```10 meters``` – kishorer747 Nov 29 '16 at 09:51
  • @kishorer747 wow, i'm just going to implement that manually if I didn't read your comment! Feel silly now. – Moses Aprico Jul 06 '17 at 13:54
  • 1
    Battery wise, @kishorer747 's way will not be efficient, if no other app is using the GPS it will check for location changes. – Ohad Zadok Jul 06 '17 at 15:31
  • just asking using setFastestInterval is there any probability to occur signal jammed? because we are getting signals from every where if we standing on a city or crowded area? – Ashana.Jackol Oct 17 '17 at 03:50
  • you are getting the location produced by the device so I guess the answer is no. – Ohad Zadok Oct 17 '17 at 15:32
  • If you don't call this method, a fastest interval will be selected for you. It will be a value faster than your active interval (setInterval(long)). – Saeid Mohammadi Jan 22 '22 at 08:09
10

Also: Is there an inbuilt function to check the location updates only if the distances of the first update are more than 20meters with the second update?

LocationRequest has a method you can use to set the minimum distance between updates.

int minimumDistanceBetweenUpdates = 20;

LocationRequest request = new LocationRequest();
request.setMinimumDisplacement(minimumDistanceBetweenUpdates);
// ... request.setInterval(interval); etc
user2120910
  • 404
  • 1
  • 5
  • 16
7

I am writing my own background location updates for interval of every 5 minutes in android. I would like to know the difference between setIntervaland setFastestInterval

Assume that the setFastestInterval(); has higher priority for requesting a Location. To whatever app you set the setFastestInterval(); it will be that app that will be executed first (even if other apps are using LocationServices).

ex: If APP1 has setFastestInterval(1000 * 10) and APP2 has setInterval(1000 * 10), both APPS have same request interval. But it is the APP1 that will make the first request. (this is what i have understood, the answer is not correct maybe)

When I setInterval to 5 mins and setFastestInterval to 2 mins. The location update is called every 2 mins.

If you are using setFastestInterval() together with the setInterval() the app will try to make a request for the time given in the setFastestInterval(), that's why your app makes a request every 2mins.

Also: Is there an inbuilt function to check the location updates only if the distances of the first update are more than 20meters with the second update?

For Making request every 20 meters you can create a LocationModel

    public class LocationModel {
    private double latitude;

    private double longitude;
    public LocationModel(){
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

and in the first request you set the lat and long to current location (using getLastLocation();)

then onLocationChanged() you get the data from the object and compare with the new Current Location

float distanceInMeters = distFrom((float)locationObj.getLatitude(), (float)locationObj.getLongitude(), (float)mCurrentLocation.getLatitude(), (float)mCurrentLocation.getLongitude());

using this function which is also a suggestion of users of SO

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 6371; //kilometers
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                    Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    float dist = (float) (earthRadius * c);

    return dist;
}
hrskrs
  • 4,447
  • 5
  • 38
  • 52
  • 7
    If you work with Location object from sdk, it exists a function distanceTo ==> http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location) – mrroboaat Jan 03 '15 at 14:46
3

setInterval (long millis) This interval is inexact. You may not receive updates at all (if no location sources are available), or you may receive them slower than requested. You may also receive them faster than requested (if other applications are requesting location at a faster interval). The fastest rate that that you will receive updates can be controlled with setFastestInterval(long). By default this fastest rate is 6x the interval frequency.

setFastestInterval (long millis) Unlike setInterval(long), this parameter is exact. Your application will never receive updates faster than this value. If you don't call this method, a fastest interval will be selected for you. It will be a value faster than your active interval (setInterval(long)).

Haritha Reddy
  • 105
  • 10