Android LocationManager.getLastKnownLocation() 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1608632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:20:19  来源:igfitidea点击:

Android LocationManager.getLastKnownLocation() returns null

androidgps

提问by Robert Parker

So I'm trying to sample the gps coordinates just once in an application. I don't want to create a LocationListener object to constantly get gps updates. I want to wait until receiving the coordinates, and then proceed on to another task.

所以我试图在应用程序中仅对 gps 坐标进行一次采样。我不想创建 LocationListener 对象来不断获取 gps 更新。我想等到收到坐标,然后再进行另一个任务。

Here is a code snippet

这是一个代码片段

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);

The loc variable is always null in the emulator. I tried using the command "geo fix latitude longitude" to set it, and also I tried using the DDMS way of setting it. Neither method had any effect on the code. Also the snippet isn't causing any exceptions.

loc 变量在模拟器中始终为空。我尝试使用命令“geo fix latitude longitude”来设置它,我也尝试使用 DDMS 设置它的方式。这两种方法都没有对代码产生任何影响。此外,该代码段不会导致任何异常。

Thanks for your help.

谢谢你的帮助。

采纳答案by krishc

The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.

请求更新位置的调用不会阻塞,因此它不会在那里等待。此外,模拟器中的提供程序可能尚未启动。

A possible check could be to see if the settings in it disable gps provider ? then send geo fix.

一个可能的检查可能是查看其中的设置是否禁用了 gps 提供程序?然后发送地理修复。

However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.

但是,我会使用位置侦听器,这在您的情况下是理想的,因为您需要进行地理修复才能进一步进行。位置侦听器用于在位置更改时接收来自 LocationManager 的通知。您可以在第一次地理定位后取消注册侦听器。

Note: It can take some time on device to get current location, and even on device this can return null.

注意:在设备上获取当前位置可能需要一些时间,甚至在设备上也可能返回 null。

回答by I82Much

Try using the MyLocationOverlay, create a runnable that does what you need to do with that GPS location, and pass it to

尝试使用MyLocationOverlay,创建一个 runnable 来执行您需要对该 GPS 位置执行的操作,并将其传递给

boolean     runOnFirstFix(java.lang.Runnable runnable)
          Queues a runnable to be executed as soon as we have a location fix.

and then disable the location updates for the MyLocationOverlay.

然后禁用 MyLocationOverlay 的位置更新。

Edit: The reason the location is null is because at the time that code is run, no geofix has been received.

编辑:位置为空的原因是因为在运行代码时,没有收到地理定位。