Android onLocationChanged只调用一次,不自动更新

Android onLocationChanged只调用一次,不自动更新,android,android-location,Android,Android Location,在android应用程序中,要通过按钮获取位置,请单击此处是我的代码 public class GPSTracker extends Service implements LocationListener { private final Context mContext; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = f

在android应用程序中,要通过按钮获取位置,请单击此处是我的代码

  public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

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;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}


/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

public void onLocationChanged(Location location) {

              this.location=location;
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}

我的调用函数从上面的代码中获取纬度和经度

double lat = gps.getLatitude();
double log = gps.getLongitude();
但它只能给出相同的位置,即使位置发生了变化


任何人都可以帮助我…提前谢谢..

我查看了您的代码,它似乎完全正确。请您尝试将最小距离设置为0,因为您已经给出了最小距离10米,可能是您没有以如此快的速度移动,并且您的服务可能将最小距离作为主要点,而不是最小时间

@Dynamo:requestupdatelocation调用onLocationChanged回调方法。这并不完全取决于位置分配。位置值将根据用户位置更新而更改

这里的问题是“onLocationChanged”回调方法没有得到对给定参数的调用。

试试这个

public void onLocationChanged(Location location) {
   this.location = location;
}

还可以尝试将更新的“最小距离”更改为最小值。

您的代码似乎是正确的。我能想到的唯一原因是设备无法获取位置。设备锁定您的位置需要几分钟的时间,所以请尝试给它一些时间


您可以使用adb shell dumpsys location检查哪些应用程序注册了位置侦听器,以及您的应用程序是否在其中。它还会告诉你安卓最后一次更新位置是什么时候。另外,请尝试运行任何其他位置应用程序,以查看此问题是否与您的应用程序或环境有关。

onLocationChanged(location-location)方法在更改位置后也不会调用。问题出在哪里。@chimbu:我不知道您的应用程序中的确切问题是什么,因为您的代码似乎很完美。在我的一个应用程序中,我编写了相同的代码,它正在工作。是的,但有一件事,若你们把这段代码放在后台服务中,那个么它有时就不起作用了,因为我遇到了那个问题。很抱歉,我不能在这里帮助你(另一个问题:当我通过调用显示运行时错误的方法“java.lang.RuntimeException:每个线程只能创建一个循环器”获得纬度、经度时)另一个问题:当我通过调用显示运行时错误的方法获得纬度、经度时“java.lang.RuntimeException:每个线程只能创建一个循环器”另一个问题:当我通过调用显示运行时错误的方法获得纬度、经度时,“java.lang.RuntimeException:每个线程只能创建一个循环器”