问题
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);
}
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.
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.
Location Permissions have been given as well.
Can anyone tell me possible reasons for this issue?
回答1:
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
.
回答2:
try to place LocationCallback to "onCreate()" method.
来源:https://stackoverflow.com/questions/50870587/locationcallback-not-getting-called