我有一个使用 Google map 和新的 Fuse Location API 的应用程序。我使用此代码在位置更改时获取更新:
locationClient.requestLocationUpdates(new LocationRequest(),
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//Some code
}
});
我的 Galaxy Nexus 和 Android 4.3 没问题。但是,在我 friend 的 Android 4.0.4 手机 (Xperia ST21i) 中,我无法收到这些更新。
我在 Android 文档和 StackOVerflow 上的许多(如果不是全部)问题上看到使用 LocationRequest.create()
而不是 new LocationRequest()
。这个新代码在我的手机上仍然可以正常工作;我还没有在我 friend 的手机上试过。
阅读文档,我的猜测是 LocationRequest.create()
就像:
public static LocationRequest create() {
LocationRequest locationRequest= new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setFastestInterval(aNumber);
locationRequest.setInterval(anotherNumber);
return locationRequest;
}
这两段代码有什么区别,为什么它在我的手机上有效,但在我 friend 的手机上却无效。在我 friend 的手机上更改后是否可以使用?
如果没有,我还需要做什么才能让它发挥作用?
最佳答案
实际上在许多 4.0.x 设备中存在一个问题,如果您依赖于网络级别的位置,则 OnLocationChanged
不会被正确调用。
在 AOSP Issue Tracker 发布报告很可能是你所看到的
如那里所述,基本上网络级位置从不刷新,并且由于您使用的是 LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
,它会尽可能使用最不突兀的方法为您提供更新。我确实觉得有点奇怪,如果 GPS 和 Wifi 在该设备上也可用,你仍然得不到更新。
解决此问题的一种可能方法是将优先级更改为 LocationRequest.PRIORITY_HIGH_ACCURACY
。如果您认为该解决方案会给您的用户带来麻烦,您可以执行一些操作,例如使用 PRIORITY_HIGH_ACCURACY
创建一个新的 LocationRequest
,并且仅在您的第一种方法失败时才使用它。
特别是,我会在您首次发起位置请求时设置一个计时器,并在您的 LocationListener
中使用一个标志来指示在该时间范围内是否收到任何结果。如有必要,可以提供代码示例。
关于android - LocationRequest 和 LocationClient.requestLocationUpdates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250220/