【问题标题】:Android network provider and gps provider returning nullAndroid 网络提供商和 GPS 提供商返回 null
【发布时间】:2014-05-04 18:49:53
【问题描述】:

我已经在我想要获取经度和纬度的地方完成了这段代码,但是使用网络提供商和 gps 提供商我得到了我的位置 null 。即使启用了 gps ......为什么会这样?

      boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
      boolean  isNetworkEnabled = lm
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Location location = null;
        if (!isGPSEnabled && !isNetworkEnabled)
        {  
        }
        else if(isGPSEnabled ) {
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location == null)
            {                   
                if(isNetworkEnabled)
                    location =                                   lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);                 
                if(location == null)
                {
                }
            }
        } else if(isNetworkEnabled){
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
            if(location == null)
            {
            }
        }

【问题讨论】:

标签: android gps-time


【解决方案1】:

没有得到坐标有几个原因,但如果你的,因为你的 gps 已启用 (remember gps takes alot of time to get coordinates) 并且你首先在代码中检查 gps,它不是转到 network 部分因为您正在使用 if else.so 将您的代码更改为这样并首先检查网络。

if (!isGPSEnabled && !isNetworkEnabled)
        {  
        }
      else if(isNetworkEnabled){
            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);   
            if(location == null)
            {
            }
        }
        else if(isGPSEnabled ) {
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location == null)
            {                   
                if(isNetworkEnabled)
                    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);                 
                if(location == null)
                {
                }
            }
        } 

【讨论】:

  • 我的错误似乎出现在索尼手机上,有什么不同吗?在所有情况下,我都得到 0 - 0 ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
相关资源
最近更新 更多