6

I'm using FusedLocationAPI it is working properly but when user disabled location i'm using SettingsApi to show dialog to user to enable location.

In marshmallow and above devices it is working fine but in kitkat if location is enabled or disabled, it is always triggering LocationSettingsStatusCodes.RESOLUTION_REQUIRED.

  LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult locationSettingsResult) {

            final Status status = locationSettingsResult.getStatus();
            Log.e("status",status.getStatusCode()+"\n"+status.getStatus()+"\n"+status.getStatusMessage());
            final LocationSettingsStates LS_state = locationSettingsResult.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    startLocationUpdates();

                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);

                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.

                    break;
            }
        }
    });



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PERMISSION_SETTING) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, permissionsRequired[0]) == PackageManager.PERMISSION_GRANTED) {
            //Got Permission
            proceedAfterPermission();
        }
    } else if (requestCode == REQUEST_CHECK_SETTINGS) {
        switch (resultCode) {
            case RESULT_OK:
                // All required changes were successfully made
                startLocationUpdates();
                break;
            case RESULT_CANCELED:
                // The user was asked to change settings, but chose not to
                Toast.makeText(this, "User cancelled location enabled", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}

In kitkat it is showing dialog like thisimage but when clicking on ok it is triggering user cancelled location

Dhanumjay
  • 540
  • 7
  • 17

0 Answers0