locationManager.getLastKnownLocation()始终在新的xperia X性能

时间:2016-10-28 10:55:47

标签: android location android-6.0-marshmallow android-gps sony-xperia

我正在创建一个需要知道用户粗略位置的应用,而且我测试过的其他每部手机都可以正常使用,但在我的xperia X性能上,getLastKnownLocation()始终返回null,无异常

此代码适用于其他设备,gps可在我的手机上与其他应用一起使用。

  public boolean startLocationManager() {
    // Check for permission to use Fine Location
    if (ContextCompat.checkSelfPermission(mActivity,
            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        try {
            locationManager = (LocationManager) mActivity.getSystemService(LOCATION_SERVICE);

            String providerInfo = null;

            // Check if Network provider is enabled
            if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                providerInfo = LocationManager.NETWORK_PROVIDER;
            }
            else{
                showSettingsAlert();
            }

            // Application can use GPS or Network Provider
            if (providerInfo != null) {
                locationManager.requestLocationUpdates(
                        providerInfo,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this
                );

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                }

                if(location != null){
                    isEnabled = true;
                }
                else{
                    Toast.makeText(mActivity, R.string.location_is_turned_off, Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "Can't connect to LocationManager", e);
        }
    }
    return isEnabled;
}

我已经为该应用授予了正确的权限,但我仍然无法从getLastKnownLocation()获得任何空格。

我不知道它是否有任何帮助但是有一些电话信息: Phone info

2 个答案:

答案 0 :(得分:0)

使用以下代码。

 public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

答案 1 :(得分:0)

if (providerInfo != null) {
    locationManager.requestLocationUpdates(
        providerInfo,
        MIN_TIME_BW_UPDATES,
        MIN_DISTANCE_CHANGE_FOR_UPDATES,
        this
    );

if (locationManager != null) {
    location = locationManager.getLastKnownLocation(
        LocationManager.NETWORK_PROVIDER);
}

无法保证从getLastKnownLocation获取位置,因为它会检索先前缓存的位置(如果有)。你必须等到onLocationChanged回调被解雇