【问题标题】:Why doesn't onLocationChanged method get called in service?为什么不在服务中调用 onLocationChanged 方法?
【发布时间】:2014-03-18 13:04:05
【问题描述】:

我以纬度和经度的形式获取我当前的位置。我的问题是,一旦我得到坐标,当我移动到另一个位置时它就不会更新。我认为 onLocationChanged 方法不会被调用。我在这里读到http://www.lengrand.fr/2013/10/onlocationchanged-is-never-called-on-android/ 它不会被自己调用。我已经阅读了大部分教程,但找不到解决方案。那么如何在我的服务中调用 onLocationChanged 方法。请一步一步给我建议。

我的代码如下:

  public   class GPSTracker extends Service implements LocationListener {
 public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
}



             @Override
public void onLocationChanged(Location location) {

    getLocation();

}

【问题讨论】:

    标签: android service gps location


    【解决方案1】:
    • 确保您从 LocationManager 请求LocationUpdates(...)(在您的情况下,您似乎这样做了)
    • 如果您在建筑物内并且您的设备没有获得 GPS 修复 onLocationChanged(...) 未被调用,我遇到了同样的问题(在我家中调试时),但在外面我看到该函数被调用了一次已检索 GPS 定位。

    【讨论】:

      【解决方案2】:

      试试

          @Override
             public void onLocationChanged(Location location)
             {
                Toast.makeText(getApplicationContext(), "My Position !!!"+ location.getLatitude + location.getLongitude,
                      Toast.LENGTH_LONG).show();
             }     
      

      【讨论】:

      • 这可能会导致不良行为。根据更新频率,onLocationChanged() 可能会在当前 Toast 完成之前触发,并最终导致排队的 Toast 积压,即使在请求停止后仍会继续出现很长时间。
      【解决方案3】:

      奇怪的是,您甚至只获得了一个位置更新。从您的代码看来,当您收到位置更改时调用 getLocation(),但随后您只在 getLocation() 函数内设置您的位置请求(即,您永远不应该输入 getLocation() 函数)。这就是我为 gps 数据实现位置侦听器的方式: 注意:我在服务中使用了它,但我的服务没有实现 LocationListener。

      private void setupLocationUpdates() {
      
          // Acquire a reference to the system Location Manager
          mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
          // Define a listener that responds to location updates
          mLocationListener = new LocationListener() {
              // Called when the location has changed.
              public void onLocationChanged(Location location) {
                  mLocation = location;
                  }
              }
              // Called when the provider is disabled by the user.
              public void onStatusChanged(String provider, int status, Bundle extras) {
                  Log.d(TAG, "Status Changed: " + String.valueOf(status) + " Provider:" + provider);
              }
              // Called when the provider is enabled by the user.
              public void onProviderEnabled(String provider) {
                  if(provider.equalsIgnoreCase("gps")) setNotification(Constants.GPS_ENABLED);
                  Log.d(TAG, "Provider: " + provider + " ENABLED");
              }
              // Called when the provider status changes.
              public void onProviderDisabled(String provider) {
                  if(provider.equalsIgnoreCase("gps")) setNotification(Constants.GPS_DISABLED);
                  Log.d(TAG, "Provider: " + provider + " DISABLED");
              }
            };
          mlocationProvider = LocationManager.GPS_PROVIDER;
          // Register for location updates 
          mLocationManager.requestLocationUpdates(mlocationProvider, 0, 0, mLocationListener);
          // Add a GPS Listener to notify when searching for signal and when fix is acquired
          mGpsListener = new GpsListener();
          mLocationManager.addGpsStatusListener(mGpsListener);
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-05
        • 2017-08-04
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 2019-11-14
        • 2014-12-08
        • 1970-01-01
        • 2013-07-22
        相关资源
        最近更新 更多