安卓LocationManager.getLastKnownLocation()返回null [英] Android LocationManager.getLastKnownLocation() returns null

查看:899
本文介绍了安卓LocationManager.getLastKnownLocation()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想品尝GPS应用程序中的坐标只有一次。我并不想创建一个LocationListener的对象不断得到GPS的更新。我想等到收到的坐标,然后继续到另一个任务。

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.

下面是一个code段

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);

组委会变量总是在模拟器空。我试着用命令地理修复经纬度来设置它,我也尝试过使用设置它的DDMS方式。无论是方法对了code任何影响。另外,段没有引起任何异常。

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.

感谢您的帮助。

推荐答案

要求更新的位置不阻止呼叫,因此它不会在那里等候。此外,在仿真器的提供者可能没有被启动。

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.

一个可能的检查可能是,看它的设置禁用GPS提供商?然后发送地理修复。

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

不过,我会用位置监听器,因为你需要一个地缘修复进行further.Location监听器是用于从LocationManager接到通知时的位置发生了变化,将是理想的你的情况。你可以先geofix后注销监听器。

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.

请注意:这可能需要一些时间对设备获取当前位置,甚至在设备这可以返回null

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

这篇关于安卓LocationManager.getLastKnownLocation()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆