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

好久不见. 提交于 2019-12-04 18:18:58

问题


I want to implement a dialog that ask users to decide if want to turn on location wifi and gps, but onActivityResult it's always return 0 ,even if ok button is pressed, more detail: when wifi and gps are off ,return 0, but both are properly activated (ok button pressed). when gps it is on and wifi it off, return 0 ,but wifi it properly activated (ok button pressed). only when wifi it is on ,and gps it is off, return -1 ,but gps it properly activated (ok button pressed).

here 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 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 can you help me,I really need it.

来源:https://stackoverflow.com/questions/34211904/why-onactivityresult-always-return-0-even-if-ok-button-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!