解决getLastKnownLocation返回null

android - getLastKnownLocation returns null - Stack Overflow

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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,545评论 0 13
  • 本文重点介绍应用程序的启动过程,应用程序的启动过程实际上就是应用程序中的默认Activity的启动过程,本文将详细...
    天宇sonny阅读 423评论 1 0
  • 懒得处理样式了, 将就着看吧. 官网地址: https://developer.android.com/topic...
    Reddington_604e阅读 1,740评论 0 1
  • Which Clang Warning Is Generating This Message? (http://f...
    喜欢就可以阅读 3,033评论 1 1
  • 一个满肚子火气的办公室小职员,本以为在重压面前会像一个不会游泳的人跳进水中一样,除了痛苦的挣扎和绝望什么都没有,但...
    萌芽论坛阅读 341评论 1 4