【问题标题】:onLocationChanged only called once, not refreshingonLocationChanged 只调用一次,不刷新
【发布时间】:2013-01-08 08:49:06
【问题描述】:

由于某种原因,我制作了一个小 gps 应用程序 onLocationChanged 不刷新,它只运行一次,在应用程序启动时。

这是我的代码:

public BackgroundLocationService() {
        super("myintentservice");

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

        Criteria crt = new Criteria();
        crt.setAccuracy(Criteria.ACCURACY_FINE);
        String bestProvider = locManager.getBestProvider(crt, true);

        boolean gps_enabled;
        boolean network_enabled;
        boolean best_enabled;

        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        best_enabled = locManager.isProviderEnabled(bestProvider);


        if (best_enabled) {
            locManager.requestLocationUpdates(bestProvider, 15000, 20, locListener);
            Log.i(TAG, "Best enabled: " + bestProvider);
        } else {
            Log.i("Location Provider: ", "best not enabled: " + bestProvider);
            if (gps_enabled) {
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
                Log.i(TAG, "gps enabled!");
            }
            if (network_enabled) {
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
                Log.i(TAG, "network enabled!");
            }
        }

    }



    class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {

            if (location != null) {

                //locManager.removeUpdates(locListener);
                longitude = Double.toString(location.getLongitude());
                latitude = Double.toString(location.getLatitude());

                Log.i(TAG,"Location has CHANGED!");
            }
        }
        public void onProviderDisabled(String arg) {}
        public void onProviderEnabled(String arg) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }

“位置已更改!”每次应用启动时,消息仅显示一次。 我在办公室,但我可以从我的位置移动至少 5-10 米,并且所有提供商都已启用,所以它一定没问题,不是吗?

有什么想法吗?

【问题讨论】:

  • 我认为你应该指定更少的米:locManager.requestLocationUpdates(bestProvider, 15000, 5, locListener); (例如)

标签: android android-location


【解决方案1】:

当您注册位置侦听器时,您请求的更新时间最短为 15 秒,最短距离为 20 米。由于您在办公室内,因此您不太可能获得足够准确的 GPS 定位来实际检测 5-10 米的移动。你试过在外面调试吗?同样,如果您启用了模拟位置,请将模拟坐标发送到模拟器,以确保您的应用在位置更新时按预期运行。

【讨论】:

    【解决方案2】:

    试试 -

        locManager.requestLocationUpdates(bestProvider, 0, 0, locListener);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-06
      • 2011-02-04
      • 1970-01-01
      • 2011-08-06
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多