为什么onActivityResult总是返回0,即使按下确定按钮?

时间:2015-12-10 21:17:51

标签: android location google-play-services

我想实现一个对话框,要求用户决定是否要打开位置wifi和gps,但是onActivityResult它总是返回0,即使按下了ok按钮, 更多详情: 当wifi和gps关闭时,返回0,但两者都被正确激活(按下确定按钮)。 当gps打开并关闭wifi时,返回0,但w​​ifi正确激活(按下确定按钮)。 只有当wifi打开时,gps才会关闭,返回-1,但gps正确激活(按下确定按钮)。

这里是我的代码:

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;
                }
            }
        });
    }

和onActivityResult方法:

@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;
        }
    }

我希望你能帮助我,我真的需要它。

0 个答案:

没有答案