Why does onActivityResult always return 0, even if the ok button is pressed?

I want to implement a dialog box in which they ask users to decide whether you want to enable wifi and gps in the location, but onActivityResult always returns 0, even if the ok button is pressed, in more detail: when wifi and gps are turned off, return 0, but both of them are correct activated (ok button pressed). when gps it is on and off, return 0, but wifi it is activated correctly (ok button is pressed). only when Wi-Fi is on and gps is turned off, it returns -1, but gps is activated correctly (ok button is pressed).

here is my code:

public void activategps(){
        builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);


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

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location
                        // requests here.
                        //...
                        Log.i("sms", "estan disponibles");
                        Toast.makeText(MainActivity.this, "HOLA 1", Toast.LENGTH_SHORT).show();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        Log.i("sms", "Motrando popup");
                     //  if( !state.isNetworkLocationPresent()|| !state.isLocationUsable()){

                            Toast.makeText(MainActivity.this, "HOLA 2", Toast.LENGTH_SHORT).show();
                            try {
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                status.startResolutionForResult(
                                        MainActivity.this,
                                        REQUEST_CHECK_SETTINGS);
                            } catch (SendIntentException e) {
                                // Ignore the error.
                            }
                      // }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Toast.makeText(MainActivity.this, "HOLA 3", Toast.LENGTH_SHORT).show();
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        //...
                        break;
                }
            }
        });
    }

and here is the onActivityResult method:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      //  final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
        switch (requestCode) {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        // All required changes were successfully made
                        Log.i("sms", "Se se activaron correctamente");
                       // Toast.makeText(MainActivity.this, "Gps fue Encendido"+Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i("sms", "No se activaron ");
                        // The user was asked to change settings, but chose not to, 0
                       // Toast.makeText(MainActivity.this, "Gps fue Omitido"+Activity.RESULT_CANCELED, Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
                break;
        }
    }

I hope you help me, I really need it.

+4

Source: https://habr.com/ru/post/1619412/


All Articles