addOnFailureListener() doesnot call if user once enabled and after sometime disable location. when using LocationServices.getSettingsClient()

Viewed 113
  1. I have used LocationServices.getSettingsClient() to remove depricated
    LocationServices.SettingsApi.checkLocationSettings().
  2. It is asking user to enable location first time when mainActivity launch if location is disabled.
  3. If in between i disable location . it is not asking to enable location. means
    result.addOnSuccessListener is called even if my location is disabled.
  4. if Location is disabled inbetweeen it should ask result.addOnFailureListener
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
        mSettingsClient = LocationServices.getSettingsClient(getContext());
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        mLocationSettingsRequest = builder.build();
        mLocationService = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        Task<LocationSettingsResponse> result =
                LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
  result.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
               
              LocationSettingsResponse response =
                            task.getResult(ApiException.class);

                    LocationSettingsStates locationSettingsStates = response.getLocationSettingsStates();
                    if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {
          //check point 4 
            }
        });

        result.addOnFailureListener(getActivity(), new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(getActivity(),
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                }
            }
        }); 

  1. here I want need to open location enabling dialog using following code:
final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }

5 .but using SettingsClient onSucess gives Task<LocationSettingsResponse> result which does not give statusCode that I recieve in catch block

Update

First Tag is try part of OnComplete() when location is by default on second time I turn off location and go to other Fragment and again came to main fragment. Tt executed try part even I location was disabled

enter image description here

0 Answers
Related