android位置获取空值需要解决方案

android位置获取空值需要解决方案,android,gps,location,Android,Gps,Location,在上面的代码中,我的位置总是为空,即使我的gps设备处于打开状态,并且在建筑物内外,我只获得空值,请帮助我运行此代码,因为您的设备没有最后已知的位置,这就是原因。首先为onLocationChanged()生成代码。它将获取当前位置并将其另存为lastnownlocation。那么这个方法就行了 您可以在此处获取当前位置的帮助 像这样尝试你的活动必须像这样 活动扩展MapActivity实现LocationListener String logloc =""; LocationManager l

在上面的代码中,我的位置总是为空,即使我的gps设备处于打开状态,并且在建筑物内外,我只获得空值,请帮助我运行此代码,因为您的设备没有最后已知的位置,这就是原因。首先为
onLocationChanged()
生成代码。它将获取当前位置并将其另存为
lastnownlocation
。那么这个方法就行了

您可以在此处获取当前位置的帮助


像这样尝试你的活动必须像这样

活动扩展MapActivity实现LocationListener

String logloc ="";
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    logloc = String.valueOf(latitude)+","+String.valueOf(longitude);
    notfi(logloc); 
}

是否在清单文件中添加权限?此权限已添加到标记后。在处理之前,首先检查GPS或网络提供商是否已启用。请添加访问权限和位置权限。谢谢您的回复,lm呢?我们使用lm获取getLastKnownLocation。getLastKnownLocation您的代码很好,但此时无需使用locationlistner和自动触发OnLocationChanged来获取当前lat和long
 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 String provider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        lat = (double) (location.getLatitude());
        lng = (double) (location.getLongitude());

        Log.i(TAG, "Lattitude:" + lat);
        Log.i(TAG, "Longitude:" + lng);

    }