簡體   English   中英

Android 8.0中的位置值為null

[英]Location value null in Android 8.0

我正在小於Android 8.0中獲取當前位置。 但是在Android Oreo版本中,我得到的是空值。 我正在使用LocationManager來獲取位置。 我在清單文件中添加了ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION權限。 我想我錯過了一些許可。

這是我的代碼

@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以獲取當前位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM