getLastKnownLocation 使用 GPS_PROVIDER 和 NETWORK_PROVIDER 返回 NULL [英] getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER

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

问题描述

这是我的 GPSTracker 构造函数类:

This is my GPSTracker constructor class:

    public GPSTracker(Context context) {
    this.mContext = context;
    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

gpsLocation 和 networkLocation 始终为 NULL.

gpsLocation and networkLocation are always NULL.

getLastKnowLocation 方法不返回任何位置...即使我确定在午餐活动前几秒钟(并创建 GPSTracker 对象)设备具有网络位置(在检查网络位置后我已禁用地理标记可用)

getLastKnowLocation method doesn't return me any Location... even if I'm sure that few second before lunching activity (and creating GPSTracker object) the device had network location (I've disabled geotagging after checking that a network location was available)

使用 Android Studio 在 Nexus 5 上测试

Tested on Nexus 5 with Android Studio

推荐答案

是的,getLastKnownLocation() 可能返回 null.文档 说:

Yep, getLastKnownLocation() may well return null. The documentation says:

返回一个位置,指示来自最后一个已知位置的数据从给定的提供程序获得修复.

Returns a Location indicating the data from the last known location fix obtained from the given provider.

这可以在不启动提供程序的情况下完成.请注意,这位置可能已过时,例如,如果设备被转动关闭并搬到另一个位置.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

如果提供者当前被禁用,则返回 null.

If the provider is currently disabled, null is returned.

Android 设备不会一直自动跟踪其位置.最后已知位置"仅在某些应用程序请求该位置时才可用.因此,您不应期望始终使用 getLastKnownLocation() 获取位置.即使它返回一些东西,位置也可能不是最新的.Location 对象有一个时间戳,您可以使用 getTime() 方法读取该时间戳,以及您可以使用 getAccuracy() 读取的估计准确度方法.这些可用于评估可用位置.

An Android device does not automatically track its location all the time. The "last known location" is available only if some application has requested the location. So you should not expect to always get a location with getLastKnownLocation(). And even if it returns something the location might not be up-to-date. The Location object has a timestamp which you can read with the getTime() method and an estimated accuracy which you can read with the getAccuracy() method. These can be used to evaluate is the location usable.

如果您收到 null 或一个非常旧的位置,则需要请求一个新的位置.基本选项只是请求单个位置更新或具有您可以指定的时间间隔的连续更新.

If you receive null or a very old location you'll need to request a fresh location. The basic options are just requesting a single location update or continuous updates with a time interval that you can specify.

在任何一种情况下,您的类都需要实现 LocationListener 接口:

In either case your class needs to implement the LocationListener interface:

public class GpsTracker implements LocationListener {

    // ...other code goes here...

    @Override
    public void onLocationChanged(Location location) {

        // Do something with 'location' here.   

    }
}

对于单个位置更新,您将调用 [LocationManager.requestSingleUpdate()](http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper)) 和持续更新 [LocationManager.requestLocationUpdates()](http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)).在任何一种情况下都会有一个小的(或不那么小的)延迟和 LocationListener.onLocationChanged() 回调在位置可用时调用.

For a single location update you would call [LocationManager.requestSingleUpdate()](http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper)) and for continuous updates [LocationManager.requestLocationUpdates()](http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)). In either case there will be a small (or not so small) delay and the LocationListener.onLocationChanged() callback is called when a location is available.

例如,您可以像这样向 GPS 提供商请求一次更新:

For example you could request a single update from the GPS provider like this:

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);

对于 GPS 提供商的持续更新,最多每 1 秒,并且只有当位置变化至少 1 米时,您才能调用:

For continuous updates from the GPS provider max. every 1 second and only when the location has changed at least 1 meter you could call:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

然后还有一些 requestSingleUpdate() 的更多变体,您可以在文档中看到,另一方面是 Google Play 服务位置 API,但我们不要去那里.您的问题是关于 getLastKnownLocation() 返回 null 和 Stack Overflow 有几个不同方式请求最新位置的示例.

And then there are some more variations of the requestSingleUpdate() which you can see in the documentation and on the other hand the Google Play services location APIs but let's not go there. Your question was about getLastKnownLocation() returning null and Stack Overflow has several examples of different ways to request an up-to-date location.

这是一个旧答案.基本思想仍然有效,但请查看 Android 文档,了解请求位置更新的现代方法.

This is an old answer. The basic idea is still valid, but check out the Android documentation for modern ways to request location updates.

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

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