android: 距离变化较大时获取位置

android: Getting location when there is considerable change in distance

我想定期获取当前位置的详细信息,当与之前的位置有相当大的变化时,即使应用程序未打开,它也应该更新。我没有看到任何样本可以做到这一点。请告诉我如何完成(如果我错了请纠正我)

This 是一个不错的 android 库`

LocationTracker tracker;
TrackerSettings settings =
                new TrackerSettings()
                        .setUseGPS(true)
                        .setUseNetwork(true)
                        .setUsePassive(true)
                        .setTimeBetweenUpdates(1000)
                        .setMetersBetweenUpdates(1);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
tracker = new LocationTracker(this, settings) {

            @Override
            public void onLocationFound(Location location) {
                // Do some stuff when a new location has been found.

                //mMap.clear();

                Toast.makeText(MapsActivity.this,"Location :"+location.describeContents()+location.getSpeed()+"\n"+location.getAltitude()+"\n"+location.getLatitude()+"\n"+location.getLongitude()+"\n"+location.getProvider()+"\n"+location.getAccuracy(), Toast.LENGTH_SHORT).show();

                updateLocation(location);
                //updateLocationInMap(location);
                setLocationInMapFromFireBase();

            }

            @Override
            public void onTimeout() {

                Toast.makeText(MapsActivity.this, "Time out", Toast.LENGTH_SHORT).show();
            }
        };
        tracker.startListening();` 

非常简单

您可以使用以下方法设置接收位置更新的最小距离:https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)

此外,您可以使用 Service that runs regardless whether the app is open or closed. You can make this service start up whenever the app opens and/or the device boots up. For more info on that look at this question: Trying to start a service on boot on Android

收听位置更新

这样它总是 运行 并且会根据您的要求接收位置更新。