Android LocationRequest setInterval(长毫秒)和LocationRequest setFastestInterval(长毫秒)的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26114345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:04:24  来源:igfitidea点击:

Difference between LocationRequest setInterval (long millis) and LocationRequest setFastestInterval (long millis)

androidgpslocationlocationmanager

提问by TheDevMan

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

我正在 android 中每隔 5 分钟编写一次我自己的后台位置更新。我想知道 setInterval 和 setFastestInterval 之间的区别

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

当我 setInterval 为 5 分钟,setFastestInterval 为 2 分钟时。位置更新每 2 分钟调用一次。

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

我想知道区别。我无法理解这个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?

另外:是否有内置功能来检查位置更新,仅当第一次更新与第二次更新的距离超过 20 米时?

Thanks!

谢谢!

回答by Ohad Zadok

Based on the relevant Android documentation:

基于相关的Android文档

  • 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).
  • setInterval(long)意味着 - 设置您想要获取位置的间隔。
  • setFastestInterval(long)意味着 - 如果一个位置可用,您可以更快地获取它(即另一个应用程序正在使用位置服务)。

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

例如,您启动您的应用程序并通过 注册它 setInterval(60*1000),这意味着您将每 60 秒获得一次更新。

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).

现在你打电话setFastestInterval(10*1000)。如果您是唯一使用定位服务的应用程序,您将继续大约每 60 秒收到一次更新。如果另一个应用程序使用更新率更高的位置服务,您将获得更多位置更新(但不会更频繁地每 10 秒一次)。

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.

我相信它对你的应用消耗的电池寿命有很好的影响,你定义了你可以等待的最长时间,同时说如果有更新可用,你想要它。电池消耗将计入请求更频繁更新的应用程序,而不是您的应用程序。

回答by user2120910

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?

另外:是否有内置功能来检查位置更新,仅当第一次更新与第二次更新的距离超过 20 米时?

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

LocationRequest 有一个方法可以用来设置更新之间的最小距离。

int minimumDistanceBetweenUpdates = 20;

LocationRequest request = new LocationRequest();
request.setMinimumDisplacement(minimumDistanceBetweenUpdates);
// ... request.setInterval(interval); etc

回答by hrskrs

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

我正在android中每隔5 分钟编写一次我自己的后台位置更新。我想知道其中的差别之间setIntervalsetFastestInterval

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).

假设setFastestInterval();具有更高的优先级来请求Location。对于您设置的任何应用setFastestInterval();程序,它将首先执行该应用程序(即使其他应用程序正在使用LocationServices)。

ex: If APP1has setFastestInterval(1000 * 10)and APP2has setInterval(1000 * 10), both APPShave same request interval. But it is the APP1that will make the first request. (this is what i have understood, the answer is not correct maybe)

例如:如果APP1setFastestInterval(1000 * 10)APP2setInterval(1000 * 10)这两个应用程序具有相同的请求间隔。但APP1将发出第一个请求。(这是我所理解的,答案可能不正确)

When I setIntervalto 5 minsand setFastestIntervalto 2 mins. The location updateis called every 2 mins.

当我setInterval5分钟,并setFastestInterval2分钟。在location update被称为每隔2分钟

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.

如果您setFastestInterval()setInterval()应用程序一起使用,应用程序将尝试在 中给出的时间内发出请求setFastestInterval(),这就是您的应用程序每 2分钟发出一次请求的原因

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?

另外:是否有内置功能来检查位置更新,仅当第一次更新与第二次更新的距离超过 20 米时?

For Making request every 20 meters you can create a LocationModel

对于每 20 米提出请求,您可以创建一个 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 latand longto current location (using getLastLocation();)

并在第一个请求中将lat和设置long为当前位置(使用getLastLocation();

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

然后onLocationChanged()你从对象中获取数据并与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

使用此功能也是对用户的建议 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;
}

回答by Haritha Reddy

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.

setInterval (long millis) 这个间隔是不准确的。您可能根本没有收到更新(如果没有可用的位置源),或者您收到更新的速度可能比请求的慢。您也可能比请求更快地接收它们(如果其他应用程序以更快的间隔请求位置)。可以使用 setFastestInterval(long) 控制接收更新的最快速度。默认情况下,此最快速率是间隔频率的 6 倍。

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)).

setFastestInterval (long millis) 与 setInterval(long) 不同,这个参数是精确的。您的应用程序将永远不会以比此值更快的速度接收更新。如果您不调用此方法,则会为您选择最快的时间间隔。它将是一个比您的活动间隔更快的值(setInterval(long))。