【问题标题】:Android using LocationManager Get Provider is "Passive" in My Moto G Android Phone使用 LocationManager Get Provider 的 Android 在 My Moto G Android 手机中是“被动的”
【发布时间】:2014-06-23 21:56:59
【问题描述】:

我需要一些关于 Android 中 LocationManager 的信息的帮助。我正在使用此代码根据用户的位置获取用户的特定信息,但在我的设备 Motorola G 我得到 provider="Passive" 以及 Location return Null My Motorola G 在最新的 Android Kit Kat(4.4) Android 操作系统上运行。

我也在其他设备中尝试此代码,因此提供者是网络或 GPS 也可以正确找到位置。

        locationManager = (LocationManager) getSherlockActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(true);
        criteria.setBearingRequired(true);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW); 
        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(provider, 1000, 0, this);

        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            onLocationChanged(location);
         } else {
             Toast.makeText(getSherlockActivity(), "Location not available", Toast.LENGTH_SHORT).show();
            }

【问题讨论】:

    标签: android gps location locationmanager


    【解决方案1】:

    像这样使用位置管理器:

    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
    
    ;
    

    【讨论】:

    • 也可以尝试此代码在其他设备上工作,但在 moto g 中不工作
    • 在其他设备上这段代码和我的代码都运行正常
    • @Dhaval 我已经在 moto 上尝试过这段代码,它工作正常。请检查设备的 GPS 设置。
    • gps 设置需要什么?如果 gps 关闭,则提供者为网络或被动。在 moto g 提供者中作为被动
    • 感谢@Ravind Maurya 解决了我在 moto G 中的问题位置设置已关闭,因此找不到位置,此设置已打开然后正常工作
    【解决方案2】:

    我知道这在某种程度上是一个老问题。但希望我的回答也能对其他人有所帮助。

    问题与使用的设备无关,实际上问题是我们如何使用提供的API。

    在下面的代码 sn-p 中,您为 enabledOnly 标志传递 true,而您只需将其传递为 false,此时它将正常工作。

    而不是: provider = locationManager.getBestProvider(criteria, true);

    成为: provider = locationManager.getBestProvider(criteria, false);

    清除这一点:

    • 如果我们将此标志“enabledOnly 标志”设置为 true,这意味着根据我们的标准从仅启用的提供者中返回请求的最佳提供者,这将返回被动提供者。

    • 如果我们将其设置为 false,这意味着即使未启用,也会根据我们的标准返回请求的最佳提供者,这将返回 gps。

    顺便说一句,我也在使用 Moto G - 但它是第 2 代 - 并且它以这种方式与我一起使用 :)

    希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      如果您的应用支持 Android 的 KitKAT(4.4) 版本,请阅读此内容 我遇到了同样的问题,我的代码在其他不运行 kitkat 的设备上运行良好,经过一番窥探后,我发现你必须明确打开 GPS 才能通过 wifi/gps 获取位置。你可以在这里读到它。 https://support.google.com/nexus/answer/3467281

      【讨论】:

      • 我没有看到相关性;对不起
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多