【问题标题】:getLastKnownLocation() returns null [closed]getLastKnownLocation() 返回 null [关闭]
【发布时间】:2015-08-20 07:21:26
【问题描述】:

我在 SO 上阅读了很多关于此主题的问答,但我不得不说它们都不起作用。

我的问题是,即使我启用了 GPS,我也无法获取位置,除非我打开 Google 地图并获取我的位置,然后返回应用程序,这绝对不是用户的选择。

我有以下函数来获取位置。

public Location getCurrentLocation() {
    LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    return myLocation;
}

关于如何解决这个问题,我有什么遗漏吗? 我也试过这个 http://developer.android.com/training/location/retrieve-current.html#last-known 但仍然返回 null。

提前致谢

【问题讨论】:

标签: android google-maps gps android-location


【解决方案1】:

关于如何解决这个问题我有什么遗漏吗?

GPS 无线电会正常断电,因为它们会消耗大量电池。因此,getLastKnownLocation() 可以经常返回null 或陈旧的位置,因为没有任何东西检查位置修复。因此,getLastKnownLocation() 仅在您对该位置有兴趣并且在没有位置时很高兴时才有用。

如果您需要知道位置,则需要使用requestLocationUpdates() 或类似的东西,以使Android 启动GPS 无线电并主动尝试查找位置。

【讨论】:

猜你喜欢
  • 2013-11-06
  • 2013-12-24
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多