Android 位置管理器返回 NULL

Android Location Manager returning NULL

我有一个简单的位置管理器,它通常可以正常工作,但是当 Android 设备关闭然后重新打开时,android 位置管理器 returns 甚至为空当我请求更新时。我知道 getLastKnownLocation 可以 return null,但我相信我正在我的代码中处理它。所有建议表示赞赏。

显然 lon = location.getLongitude();正在崩溃。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
        {
            // request location update!!
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lon = location.getLongitude();
            lat = location.getLatitude();
        }


        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //Get last known location
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //Update if not null
        if (location != null)
        {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        //Request update as location manager can return null otherwise
        else
        {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
    }

the android location manager returns Null even when I have it requesting updates

正确。请求更新请求 Android start 尝试找出设备所在的位置。这需要一段时间。同时,getLastKnownLocation()可以很好的returnnull.

getLastKnownLocation() 通常只有在您想知道位置时才有用,但如果该数据尚未准备好,您可以在没有它的情况下继续前进。如果您需要位置,并且 getLastKnownLocation() returns null,您将等待使用 Location 直到 onLocationChanged() 被调用。请注意,可能永远不会调用 onLocationChanged(),因为不要求用户启用任何位置提供程序,也不要求设备能够实际获取位置修复。

唯一的问题是我在 Android Phone 上启用了飞行模式。我现在有点尴尬!

我看到了你关于飞行模式的回复,但是你的代码确实有不好的做法。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
        {
            // request location update!!
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lon = location.getLongitude();
            lat = location.getLatitude();
        }

您已经在此处看到该位置为 null,这意味着您无法访问纬度和经度等内部属性。在任何情况下,位置请求都是异步的,它们需要很短的时间才能启动。

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //Get last known location
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //Update if not null
        if (location != null)
        {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        //Request update as location manager can return null otherwise
        else
        {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
    }

即使在这里,在 "else" 条款中,您也无法立即访问该位置。这意味着您无法立即获取位置(它只会保留最后一个值)。
你应该做的是实现 onLocationChanges 方法并以异步方式处理那里的位置。像这样:

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null)
    {
        lat = location.getLatitude();
        lon = location.getLongitude();
    }
   else
    {
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

}

@Override
public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lon = location.getLongitude();
}