事情的起因就是我用系统的GetLastKnownLocation输出结果永远是null。后来看大家说这个方法在室内90%都不管用。

于是查了国内各大网站给出了两个解决办法:

1.while(GetLastKnownLocation(provider) = null){

    GetLastKnownLocation = null

}

2.locationManager.setTestProviderEnabled(provider1, false);

然而这两种方法并没能解决我的问题。只是疯狂在while里循环,知道我的App崩溃。。。

最后查询StackOverflow得到解决办法。

用如下办法获取Last Location

LocationManager mLocationManager;Location myLocation = getLastKnownLocation();private Location getLastKnownLocation() {    mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);    List<String> providers = mLocationManager.getProviders(true);    Location bestLocation = null;    for (String provider : providers) {        Location l = mLocationManager.getLastKnownLocation(provider);        if (l == null) {            continue;        }        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {            // Found best last known location: %s", l);            bestLocation = l;        }    }    return bestLocation;}

结果很成功。

激动之余记录一下,并分享给大家。

原文链接:https://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null

更多相关文章

  1. android fragment 重复创建的问题
  2. Android(安卓)Studio 图片资源存放位置 + drawable文件夹的创建
  3. [android] 构建menu的一种方法
  4. EventBus两个Activity传值
  5. Android(安卓)API Demos学习 - Service部分
  6. Android(安卓)各层中日志打印功能的应用
  7. Android中简单调用图片、视频、音频、录音和拍照的方法
  8. android自定义title的方法
  9. Android中定时器的使用(Timer)

随机推荐

  1. 关于前端学习路线的一些建议(含面试自测题
  2. 关于 Babel 你必须知道的基础知识
  3. 继 GitHub 后微软又收购了 npm
  4. 熔断器 Hystrix 源码解析 —— 命令合并
  5. 【51CTO学员故事】6年拿下8个软考证书
  6. 微服务调用链追踪中心搭建
  7. 4.dockerfile
  8. 数据结构之集合和映射
  9. 打印机不断打印出一张张空白纸——好像跟
  10. 进阶 · 那些你必须搞懂的网络基础