简体   繁体   English

getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL

[英]getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER

This is my GPSTracker constructor class: 这是我的GPSTracker构造函数类:

    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 and networkLocation are always NULL. gpsLocation和networkLocation始终为NULL。

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) getLastKnowLocation方法不会返回任何位置...即使我确定在午餐活动之前几秒钟(并创建GPSTracker对象)设备有网络位置(我在检查网络位置可用后禁用了地理标记)

Tested on Nexus 5 with Android Studio 使用Android Studio在Nexus 5上测试

Yep, getLastKnownLocation() may well return null. 是的, getLastKnownLocation()可能会返回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. 请注意,此位置可能已过期,例如,如果设备已关闭并移至其他位置。

If the provider is currently disabled, null is returned. 如果当前禁用了提供程序,则返回null。

An Android device does not automatically track its location all the time. Android设备不会自动跟踪其位置。 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() . 因此,您不应期望总是使用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. Location对象有一个时间戳,您可以使用getTime()方法读取该时间戳,并使用getAccuracy()方法读取估计的准确度。 These can be used to evaluate is the location usable. 这些可用于评估可用的位置。

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. 基本选项只是请求单个位置更新或使用您可以指定的时间间隔进行连续更新。

In either case your class needs to implement the LocationListener interface: 在任何一种情况下,您的类都需要实现LocationListener接口:

public class GpsTracker implements LocationListener {

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

    @Override
    public void onLocationChanged(Location location) {

        // Do something with 'location' here.   

    }
}

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)). 对于单个位置更新,您可以调用[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))。 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. 在任何一种情况下,都会有一个小的(或不那么小的)延迟,并且当一个位置可用时调用LocationListener.onLocationChanged()回调。

For example you could request a single update from the GPS provider like this: 例如,您可以从GPS提供商请求单个更新,如下所示:

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

For continuous updates from the GPS provider max. 用于GPS提供商最大连续更新。 every 1 second and only when the location has changed at least 1 meter you could call: 每1秒钟,只有当位置改变至少1米时,您才可以致电:

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

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. 然后还有一些requestSingleUpdate()变体,您可以在文档中看到这些变体,另一方面可以看到Google Play服务位置API,但我们不会去那里。 Your question was about getLastKnownLocation() returning null and Stack Overflow has several examples of different ways to request an up-to-date location. 您的问题是关于getLastKnownLocation()返回null和Stack Overflow有几个不同方法请求最新位置的示例。

EDIT: This is an old answer. 编辑:这是一个老答案。 The basic idea is still valid, but check out the Android documentation for modern ways to request location updates. 基本概念仍然有效,但请查看Android文档以获取请求位置更新的现代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getLastKnownLocation()使用GPS_PROVIDER返回null - getLastKnownLocation() returns null using GPS_PROVIDER NETWORK_PROVIDER和GPS_PROVIDER在片段中返回null - NETWORK_PROVIDER and GPS_PROVIDER returns null in fragments 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER 使用OnStatusChanged从GPS_PROVIDER切换到NETWORK_PROVIDER? - Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER? 如何提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER? - How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER? 如何添加仅使用GPS_PROVIDER作为提供者而不使用NETWORK_PROVIDER触发的位置接近警报 - How to add a location proximity alert that triggers only using GPS_PROVIDER as provider and not using NETWORK_PROVIDER 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 使用GPS_PROVIDER和NETWORK_PROVIDER获取当前位置的问题 - Issue in getting Current Location with GPS_PROVIDER and NETWORK_PROVIDER 在android中,GPS_PROVIDER有效,但NETWORK_PROVIDER不起作用 - in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not Android:locationManager GPS_provider VS Network_provider - Android: locationManager GPS_provider VS Network_provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM