Android 8.0 中的位置值为空

Location value null in Android 8.0

我在小于 Android 8.0 中获取当前位置。但是在 Android Oreo 版本中,我得到的是空值。我正在使用 LocationManager 获取位置。我在清单中添加了 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限 file.I 更新鲜。我想我错过了一些许可。

这是我的代码

@SuppressLint("MissingPermission")
public void 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);

        // Try to get location if you GPS Service is enabled
        if (isGPSEnabled) {
            this.isGPSTrackingEnabled = true;

            Log.d(TAG, "Application use GPS Service");

            /*
             * This provider determines location using
             * satellites. Depending on conditions, this provider may take a while to return
             * a location fix.
             */

            provider_info = LocationManager.GPS_PROVIDER;

        } else if (isNetworkEnabled) { // Try to get location if you Network Service is enabled
            this.isGPSTrackingEnabled = true;

            Log.d(TAG, "Application use Network State to get GPS coordinates");

            /*
             * This provider determines location based on
             * availability of cell tower and WiFi access points. Results are retrieved
             * by means of a network lookup.
             */
            provider_info = LocationManager.NETWORK_PROVIDER;

        }

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

            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider_info);
                updateGPSCoordinates();
            }
        }
    }
    catch (Exception e)
    {
        //e.printStackTrace();
        Log.e(TAG, "Impossible to connect to LocationManager", e);
    }
}

尝试 Fused Location Api 获取当前位置。