2
  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

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • Have you checked by using _Log_ or debug it? – Piyush Aug 13 '21 at 07:27
  • you can check above screen shot with log first is with location enabled second is after disabling location – Kruti Bhatt Aug 13 '21 at 08:01
  • So _LocationSettingsStates_ will give you status of gps so obvious that log part will be printed because in second part you are disabling the location – Piyush Aug 13 '21 at 11:26
  • @Piyush 2nd "onComplete : try part" is after disabling gps. so isnt it should execute catch as gps is disabled ? and need to open dialog to enable location. ResolvableApiException resolvable = (ResolvableApiException) e; resolvable.startResolutionForResult(getActivity(), REQUEST_CHECK_SETTINGS); this need to execute to enable location again if user disable location and came again to main fragment – Kruti Bhatt Aug 13 '21 at 11:37
  • No. Because seeing as per code, when you disable gps you will get updated status of GPS on success of task. – Piyush Aug 13 '21 at 11:39
  • @Piyush if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {} in this condtion how to enable gps location? i need user to enable location if its disable. as you said its giving gps is not enable. how to enable from here? – Kruti Bhatt Aug 13 '21 at 11:41
  • how to use startResolutionForResult(getActivity(), REQUEST_CHECK_SETTINGS); without status? as i get status only in exception – Kruti Bhatt Aug 13 '21 at 11:44
  • In _onSuccess_ method you just need to retrieve your location using _FusedLocationClient_ and in _addOnFailListener_ you have to do these code: – Piyush Aug 13 '21 at 11:49
  • I can't paste code here. You have to write your 4 number step code in _addOnFailureListener_ – Piyush Aug 13 '21 at 11:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235972/discussion-between-piyush-and-kruti-bhatt). – Piyush Aug 13 '21 at 11:59

0 Answers0