getLastKnownLocation()即使有最好的供应商空返回 [英] getLastKnownLocation() returns null even with best provider

查看:230
本文介绍了getLastKnownLocation()即使有最好的供应商空返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code我用它来获取当前位置:

This is the Code I use to get the current location:

mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
if (best == null) {
    //ask user to enable atleast one of the Location Providers
} else {
    Location location = mgr.getLastKnownLocation(best);
//But sometimes it returns null
}

几乎每次 =最好的网络

但它不提供的位置的时候。

mgr.getLastKnownLocation(best) returns null

另外:

onResume() {
    mgr.requestLocationUpdates(best, 15000, 10, this);
}

onPause() {
    mgr.removeUpdates(this);
}

是否有这样的情况下,任何替代?

Is there any alternate for such cases?

一个选择可能是

List<String> providers = mgr.getAllProviders();

存储所有的供应商和1走1但从来没有看到这个推荐的任何地方。而且要求的最佳供应商有什么建议文档

Store all the providers and go 1 by 1. But never saw this recommended anywhere. Moreover asking for the best provider is what the docs suggest.

推荐答案

getLastKnownLocation()只有当供应商最近已用于提供程序返回一个Location对象。如果它没有被最近,Android的假设,无论是由该供应商提供的最后一个位置是过时的和错误的,并返回null。

getLastKnownLocation() only returns a Location object for a provider if that provider has been used recently. If it has not been recently, Android assumes that whatever was the last location given by that provider is out of date and wrong, and returns null.

在这种情况下,你将不得不等待您LocationListener的的 onLocationChanged()方法被调用你有一个可用的位置之前。

In such a case, you will have to wait for your LocationListener's onLocationChanged() method to be called before you have a usable location.

这篇关于getLastKnownLocation()即使有最好的供应商空返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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