【问题标题】:Android- same GPS location repeating without updatingAndroid-相同的GPS位置重复而不更新
【发布时间】:2012-08-24 16:54:17
【问题描述】:

在此处发帖之前,我已尝试对 GPS 问题进行研究。当我测试我的代码时,它一遍又一遍地重复相同的输出。方法gpsStart() 在计时器上调用。清单中添加了精细和粗略的位置权限。方法appendLog() 将输出存储在一个文件中。

public void gpsStart() {
        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Toast.makeText(getApplicationContext(), "location changed",
                // Toast.LENGTH_SHORT).show();
                // Called when a new location is found by the network location
                // provider.
                appendLog("Lat: " + location.getLatitude() + "\nLng: "
                        + location.getLongitude()+"\n");
                text3.setText("Lat: " + location.getLatitude() + "\nLng: "
                        + location.getLongitude());
            }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive location
    // updates
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

【问题讨论】:

  • 是的,我现在遇到了同样的问题。

标签: android gps location locationmanager


【解决方案1】:

您不能在计时器中调用gpsStart() 方法。我要说位置监听器是如何工作的

什么是位置监听器?

Location Listener 是在您的当前位置发生变化时通知您的类。要接收位置更新,您必须使用 LocationManager 类注册 LocationListener 类。

何时注册位置监听器?

这取决于您的应用程序的要求。例如,如果您希望位置监听器在地图上显示当前位置,那么您应该在活动的onCreate()onResume() 方法中注册位置监听器,并在onPause()onStop() 方法中取消注册接收器。如果即使您的应用程序没有运行,您也想接收位置信息,那么您可以使用服务来接收位置信息。

如何注册/取消注册位置监听器?

要注册 LocationListener,您首先需要 LocationManager 类的实例,您可以使用诸如

之类的上下文来获取它

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

之后,您必须设置位置提供程序。有两种经常使用的提供程序。

  1. GPS 提供商
  2. 网络提供商

现在要向这个位置提供者注册位置接收器,有一个 LocationManager 的方法requestLocationUpdates。在此方法中,第一个参数是提供者名称,第二个参数是请求位置更新的最短时间。第三个参数是请求位置更改的最小距离。最后一个参数是 locationListener。 这里如何使用方法

locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, minTime, minDistance, locationListener);

要取消注册位置更新,您可以使用以下方法

locationManager.removeUpdates(locationListener)

注意:正如您在问题中提到的那样,您正在计时器中调用方法gpsStart,因此每次调用此方法时都会添加位置侦听器。因此所有侦听器都在触发您新位置,因此您可能多次获得相同的位置。取而代之的是,您应该在活动开始时调用此方法一次,并在活动结束时取消注册此 locationListener。

希望你得到。 :D

享受!!!

【讨论】:

    【解决方案2】:

    最好创建 2 个侦听器。1 个用于 Gps,另一个用于网络。

    以下是代码示例

    public class MyLocationListener extends LocationListener  
    {
                public void onLocationChanged(Location location) 
               {
                    // Toast.makeText(getApplicationContext(), "location changed",
                    // Toast.LENGTH_SHORT).show();
                    // Called when a new location is found by the network location
                    // provider.
                    appendLog("Lat: " + location.getLatitude() + "\nLng: "
                            + location.getLongitude()+"\n");
                    text3.setText("Lat: " + location.getLatitude() + "\nLng: "
                            + location.getLongitude());
                }
    
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }
    
            public void onProviderEnabled(String provider) {
            }
    
            public void onProviderDisabled(String provider) {
            }
        }
    
    MyLocationListener gpsListener=new MyLocationListener();
    MyLocationListener networkListener=new MyLocationListener();
    
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpslocationListener);    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networklocationListener);
    

    【讨论】:

      【解决方案3】:

      非常感谢您的回答。 Dharmendra 你是对的,我确实需要关闭 LocationListeners,但是我的问题的答案要简单得多。我需要将最短时间(听众等待得到答案的时间)更改为大于零的值。我需要更改的值在下面以粗体显示(MINTIME)。

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, 0, gpslocationListener);        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, 0, networklocationListener);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多