简体   繁体   中英

Location value null in Android 8.0

I am getting current location in Less than Android 8.0. But in Android Oreo version, I am getting null value. I am using LocationManager for fetching the location. I have added both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the manifest file.I am fresher. I think I have missed some permission.

This is my code

@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);
    }
}

尝试融合位置Api以获取当前位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM