android:当距离发生相当大的变化时获取位置

时间:2016-09-13 14:03:42

标签: android android-gps

我希望定期获取当前位置详细信息,当之前的位置发生相当大的变化时,即使应用未打开,也应该更新。我没有看到任何样本来做这件事。请更新我怎么做(如果我错了,请纠正我)

2 个答案:

答案 0 :(得分:1)

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

此外,无论应用是打开还是关闭,您都可以使用运行的Service收听位置更新。只要应用程序打开和/或设备启动,您就可以启动此服务。有关此问题的详细信息,请参阅此问题:Trying to start a service on boot on Android

这种方式始终在运行,并会根据您的要求接收位置更新。

答案 1 :(得分:-2)

This是一个不错的安卓库。

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

非常简单