Java Android位置在启用时为空

Java Android位置在启用时为空,java,android,locationmanager,Java,Android,Locationmanager,设置经度和格度时,我得到一个空位置。我不确定我做错了什么。我在打开和关闭位置服务的情况下调试了这段代码。当它们处于启用状态时,GPS_提供者if语句为true,而当它们处于禁用状态时,两个条件都不为true,并且将转到else语句。但是,即使确认启用了提供程序,对getLastKnownLocation的调用也返回null。你知道为什么吗 private void setCoordinates(Context context) { LocationManager lm = (Locati

设置经度和格度时,我得到一个空位置。我不确定我做错了什么。我在打开和关闭位置服务的情况下调试了这段代码。当它们处于启用状态时,GPS_提供者if语句为true,而当它们处于禁用状态时,两个条件都不为true,并且将转到else语句。但是,即使确认启用了提供程序,对getLastKnownLocation的调用也返回null。你知道为什么吗

private void setCoordinates(Context context) {
    LocationManager lm = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    Location location = null;
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    } else if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        location = lm
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    } else {
        Log.e("AlarmLocation", "unable to get location");
    }

see相关答案的可能副本:不保证有任何提供商的最后已知位置。。。如果同时选中并返回null,则没有最后已知的位置。在这种情况下,您别无选择,只能注册一个回调并等待其中一个回调给您一个位置修复。您正在尝试获取当前位置吗?然后使用LocationClient