android - 重新启用定位服务
问题描述
在我禁用手机上的位置服务然后启用它们后,getLastKnownLocation()
尽管它使用的提供程序已启用,但它们返回 null。有谁知道如何解决这个问题?
另外,如果我打开谷歌地图应用程序然后返回我的应用程序,它似乎可以解决问题。
代码:
private Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
boolean enabled = mLocationManager.isProviderEnabled(provider);
@SuppressLint("MissingPermission")
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
Log.d("DebuggingLocation", "Skipped " + provider + " " + String.valueOf(enabled));
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
日志:
D/DebuggingLocation: Skipped passive true
D/DebuggingLocation: Skipped gps true
D/DebuggingLocation: Skipped network true
解决方案
通过请求被动、gps 和网络提供商的位置更新解决了问题。
推荐阅读
- google-cloud-platform - 无法连接谷歌云外部静态ip
- operating-system - 上下文切换是影响进程同步中有限等待的唯一因素吗?
- c++ - 为什么我的合并排序程序出现分段错误错误
- visual-studio-code - 通过 VSCode-Remote-SSH 终端访问时,远程机器不要求提供私人仓库的 git 用户/密码
- nativescript - NativeScript Angular .ics 文件导出
- java - Hibernate 查询不返回结果,但 CLI 上的相同查询返回 1 个结果
- python - 是否可以在 Python 中使用参数调用 getter 和 setter 属性?
- laravel - 存储外部访问/刷新令牌
- python - Pandas groupby 特定数据
- php - 将文件(来自 s3)附加到邮件