为什么不在服务中调用onLocationChanged方法?

时间:2014-03-18 13:04:06

标签: android service gps location

我以纬度和经度的形式得到我当前的位置。我的问题是,一旦我得到坐标,当我移动到另一个位置时它不会更新。我认为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();

}

3 个答案:

答案 0 :(得分:1)

  • 确保您从LocationManager请求LocationUpdates(...)(在您的情况下,您似乎这样做)
  • 如果您在建筑物内并且您的设备没有得到GPS修复onLocationChanged(...)没有被调用,我有同样的问题(在我家调试时)但是走到外面我看到该功能是在检索到GPS修复后调用。

答案 1 :(得分:0)

尝试

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

答案 2 :(得分:0)

您甚至获得单个位置更新很奇怪。从您的代码中看起来,当您收到位置更改时,您调用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);
}