I'm trying to request the current location, and if the task goes wrong, call if location is on.
private void getCurrentLocation() {
fusedClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, new CancellationToken() {
@Override
public boolean isCancellationRequested() {
return false;
}
@NonNull
@Override
public CancellationToken onCanceledRequested(@NonNull OnTokenCanceledListener onTokenCanceledListener) {
Log.d("CurrentLocation", "CancelRequest");
createLocationSettingRequest();
return null;
}
}).addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Log.d("CurrentLocation", location.toString());
mapFragment.getMapAsync(MapsActivity.this)
}
}
});
}
The problem is code in onCancelationRequested executes twice; I tried with CancellationTokenSource but I had same results.
According to docs only the first call to
cancel()
will have an effect. You can check it by addingaddOnCanceledListener()
which would be called only once.