Introduction
In my app I want to get a one-off accurate location of where the user currently is. When I used FusedLocationProviderApi.getLastLocation
sometimes this would be null or out of date location because I found out this just gets a cached location and does not request a new location update.
The solution was to request a location update only once as seen below.
LocationRequest locationRequest = LocationRequest.create()
.setNumUpdates(1)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(0);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this)
Now I get a more accurate location all the time.
My Question
How can I determine if a location update failed? Since I am only requesting 1.
When a location update is obtained this callback is invoked
@Override
public void onLocationChanged(Location location) {
}
However this does not get called if a location update failed.
What I have tried
I saw there was a ResultCallback. However, onSuccess seems to be always called even if the one-off location update failed.
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this).setResultCallback(new ResultCallbacks<Status>() {
@Override
public void onSuccess(@NonNull Status status) {
DebugUtils.log("requestLocationUpdates ResultCallback onSuccess + " + status.toString(),true,true);
}
@Override
public void onFailure(@NonNull Status status) {
DebugUtils.log("requestLocationUpdates ResultCallback onFailure + " + status.toString(),true,true);
}
});
Other
Using com.google.android.gms:play-services-location:8.4.0
Thanks for reading, please help me out.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…