LocationManager在Android中的位置返回null

我在设备上测试我的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提供程序给出null位置,那么您使用网络提供程序作为替代,尽管其准确性不是那么高。 这里有另一个问题。 您的代码应如下所示:

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

首先,检查设备状态栏上的GPS图标是否已停止闪烁,标记您实际上已锁定GPS。

如果当前未知位置,则调用返回null,例如,如果您没有GPS锁定。