How does FusedLocationProviderClient.getCurrentLocation work? CancellationToken

3.5k Views Asked by At

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.

1

There are 1 best solutions below

0
On

According to docs only the first call to cancel() will have an effect. You can check it by adding addOnCanceledListener() which would be called only once.