当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《为什么onActivityResult总是返回0,即使按下确定按钮?》经验,为你挑选了0个好方法。

我想实现一个对话框,要求用户决定是否要打开位置wifi和gps,但onActivityResult它总是返回0,即使按下确定按钮,更多细节:当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 result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

        result.setResultCallback(new ResultCallback() {
            @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;
        }
    }

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

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有