getLastKnownLocation() 返回 null [英] getLastKnownLocation() returns null

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

问题描述

我在 SO 上阅读了很多关于此主题的问答,但我不得不说它们都不起作用.

I have read a lot of Q&As on this topic here on SO but I have to say that none of them works.

我的问题是,即使我启用了 GPS,我也无法获取位置,除非我打开 Google 地图并获取我的位置然后返回应用程序,这绝对不是用户的选择.

My problem is that, even though I have GPS enabled, I cannot get a location unless I open Google Maps and get my location and then go back to the app, which is definitely not an option for the users.

我有以下函数来获取位置.

I have the following function to get the location.

public Location getCurrentLocation() {
    LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    return myLocation;
}

关于如何解决这个问题,我有什么遗漏吗?我也试过这个http://developer.android.com/training/location/retrieve-current.html#last-known但仍然返回null.

Is there anything I'm missing on how to solve this? I have also tried this http://developer.android.com/training/location/retrieve-current.html#last-known but still returns null.

提前致谢

推荐答案

关于如何解决这个问题,我有什么遗漏吗?

Is there anything I'm missing on how to solve this?

GPS 无线电会正常关闭,因为它们会消耗大量电池电量.因此,getLastKnownLocation() 可以经常返回 null 或一个陈旧的位置,因为没有任何东西检查位置修复.getLastKnownLocation() 因此,只有当您对位置有兴趣并且在没有位置时很高兴时才有用.

GPS radios are powered down normally, as they are major battery drain. Hence, getLastKnownLocation() can frequently return null or a stale location, because nothing is checking for location fixes. getLastKnownLocation(), therefore, is only useful if you have a casual interest in the location and are happy if there is no location.

如果您需要知道位置,则需要使用 requestLocationUpdates() 或类似的东西,让 Android 打开 GPS 无线电并主动尝试找到位置.

If you need to know the location, you will need to use requestLocationUpdates() or similar stuff, to cause Android to power on the GPS radio and actively try to find the location.

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

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