【问题标题】:onLocationChanged only called once, not update automaticallyonLocationChanged 只调用一次,不会自动更新
【发布时间】:2013-09-10 05:53:02
【问题描述】:

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

但即使地点发生变化,它也只会给出相同的位置。

任何人帮助我...提前谢谢..

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    我查看了您的代码,它似乎在所有方面都是正确的。您能否按照您给出的最小距离将最小距离设为 0 来尝试一下。距离 10 米,可能是您没有以那么快的速度移动,您的服务可能需要最少的时间。距离作为主要点而不是最小值。时间。

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

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

    【讨论】:

    • onLocationChanged(Location location) 方法没有被调用,即使位置发生了变化。什么问题。
    • @chimbu:我不知道您的应用程序中的确切问题是什么,因为您的代码看起来很完美。在我的一个应用程序中,我编写了相同的代码并且它正在工作。是的,但有一件事,如果您将此代码放在后台服务中,那么它有时无法正常工作,因为我遇到了这个问题。很抱歉无法在这里为您提供帮助。 :(
    • 另一个问题:当我通过调用我的方法获得纬度,经度时,显示运行时错误“java.lang.RuntimeException:每个线程只能创建一个 Looper”
    【解决方案2】:

    试试这个

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

    并尝试将 MIN_DISTANCE_CHANGE_FOR_UPDATES 减少到最小值。

    【讨论】:

    • 另一个问题:当我通过调用我的方法获得纬度,经度时,显示运行时错误“java.lang.RuntimeException:每个线程只能创建一个 Looper”
    【解决方案3】:

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

    您可以使用adb shell dumpsys location 检查哪些应用注册了位置侦听器,以及您的应用是否在其中。它还会告诉你 android 上次获得位置更新是什么时候。还可以尝试运行任何其他位置应用程序,看看这个问题是出在您的应用程序还是环境中。

    【讨论】:

    • 另一个问题:当我通过调用我的方法获得纬度,经度时,显示运行时错误“java.lang.RuntimeException:每个线程只能创建一个 Looper”
    猜你喜欢
    • 2013-01-08
    • 1970-01-01
    • 2017-01-06
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    相关资源
    最近更新 更多