gpt4 book ai didi

Android LocationManager locationManager.requestLocationUpdates() 不工作

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:13 24 4
gpt4 key购买 nike

大家好,我正在尝试通过位置管理器获取当前位置,我正在使用下面的代码

LocationManager locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider =locationManager.getBestProvider(criteria,true);

Location location =
locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

但是我没有收到这条线的任何回应

locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

并且还将 lastknown location 设为 null,我已经在 list 和动态中添加了所有权限,但是上面这行没有给出任何响应,我搜索了它但是没有得到相关答案,请帮助。

最佳答案

对于getLastKnownLocation方法,在documentation中说:

If the provider is currently disabled, null is returned.

这意味着您的 GPS 已禁用。

对于 requestLocationUpdates,您每 20 秒请求一个位置,请尝试减少此数字,以便您至少知道您的程序是否正常工作。

你可以试试这个:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

此外,我真的建议您使用新的 API 来请求位置更新:https://developer.android.com/training/location/receive-location-updates.html

它非常快速和准确,但您需要创建一个 google 开发者帐户(免费)并创建一个 api key 。所有需要的信息都应该在链接中。

关于Android LocationManager locationManager.requestLocationUpdates() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43884569/

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