java - Android 中 LocationManager 的位置返回 null

标签 java android location locationmanager

我正在设备上测试我的 Android 应用程序,但在使用时它总是失败

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

但是,如果我使用 NETWORK_PROVIDER 而不是 GPS_PROVIDER,它就可以工作。 currentLocation 返回 null。

我该如何处理这个问题?我需要在 locationListener 中添加什么具体内容吗?

谢谢!

最佳答案

GPS_PROVIDER 可能无法在有限的地方工作,假设在房子内或类似的地方。您应该以这样的方式对其进行编程:如果 GPS 提供商给出空位置,则您可以使用网络提供商作为替代方案,尽管其准确性不是那么高。 这里还有一个问题。您的代码应如下所示:

     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
{
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

关于java - Android 中 LocationManager 的位置返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188105/

相关文章:

android - 使用 DayNight 主题时更改启动屏幕中的背景颜色

ios - 监控重叠区域

swift - 如何在 Swift Joint 中创建自定义链?

java - 小程序执行无法执行打包在可执行jar内的批处理文件

java - 了解运行时无争用的同步方法

java - 在 netbeans 中构建项目时 Ivy 错误

java - 无法在 ASP.NET MVC 应用程序中使用 Java 小程序

Android Groovy 网络服务

android - Android Jetpack 是否需要使用 Kotlin 语言?

android - location 和 currentBestLocation 从哪里来?在 Android 开发指南中(获取用户位置)