问题描述
我正在使用 FusedLocationProviderClient 发出位置请求.我已将计时器保持30秒钟,以检查是否在该时间段内收到了位置信息.代码如下:
I am making a location request using FusedLocationProviderClient. I have kept a timer for 30 seconds to check if location is received within that timeframe.Code is below:
public void requestLocationUpdates() {
initializeLocationRequest();
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
startTimer();
}
private void initializeLocationRequest() {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext);
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(5000);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
synchronized (SilentCurrentLocationProvider.this) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
}
};
}
private void startTimer() {
if (mTimeout < 1) {
mTimeout = mDefaultTimeout;
}
mTimer = new Timer();
mTimer.schedule(new LocationTimer(), mTimeout * 1000);
}
在少数设备上,位置不正确.在进一步调试时,我发现正在创建 LocationRequest 和 LocationCallback ,但是它不在 onLocationResult()方法中.
On few devices, location is not coming. On debugging further, I found that LocationRequest and LocationCallback are being created, but it's not coming inside onLocationResult() method.
由于这是实时产品的一部分,因此很难在每台设备上进行调试.即使我尝试重新启动手机,也无法正常工作.用户尝试了其他应用程序,例如Ola,并且位置即将到来.
Since this is part of a live product, it's hard to debug on every device. Even I tried restarting the phone but it doesn't work. The user tried on other applications like Ola and location was coming there.
位置权限也已授予.
有人可以告诉我此问题的可能原因吗?
推荐答案
我也遇到了同样的问题.在我的情况下,设备定位方法/准确性设置为仅手机,而不是高精度或省电.将设置更改为高精度,并在locationCallback
上接收回调.
I was facing this same issue. In my case the device-location method/accuracy was set to Phone only rather than High accuracy or Battery saving. Change the settings to High accuracy and receive callbacks on locationCallback
.
这篇关于LocationCallback没有被呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!