gpt4 book ai didi

android - getLastKnownLocation 方法始终返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:11 27 4
gpt4 key购买 nike

固定位置有问题。我使用 getLastKnownLocation(LocationManager.NETWORK_PROVIDER);但总是返回 null,我已经在你的 AndroidManifest.xml 中设置了权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

并在“设置”-->“位置和安全”-->“通过网络定位”中启用。

TextView locationShow = (TextView) this.findViewById(R.id.location);
double latitude = 0.0, longitude = 0.0;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
else {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " +
location.getLongitude());
}
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
locationShow.setText("经度:" + latitude + "纬度:" + longitude);

我发现其他应用程序可以正确显示位置,所以可能我的代码有问题。

最佳答案

getLastKnownLocation() 给出最后一个有效的缓存位置。

您正在尝试从网络提供商处获取缓存位置。您必须等待几分钟才能获得有效修复。由于网络提供商的缓存是空的,您显然在那里得到了一个 null

关于android - getLastKnownLocation 方法始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6854099/

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