gpt4 book ai didi

java - Android 8.0 中位置值 null

转载 作者:行者123 更新时间:2023-12-02 11:02:05 25 4
gpt4 key购买 nike

我在低于 Android 8.0 的版本中获取当前位置。但在 Android Oreo 版本中,我得到的是 null 值。我正在使用 LocationManager 来获取位置。我在 list 文件中添加了 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);
}
}

最佳答案

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

关于java - Android 8.0 中位置值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51259932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com