无法正确调用GoogleApiClient onConnected回调

时间:2016-07-09 11:50:59

标签: java android google-drive-api google-drive-android-api

我正在尝试使用DriveApi来创建一些文件夹,并为用户上传包含一些数据的文本文件。

我尝试过实施(link)的快速入门指南,但它有一些基本问题:

  1. api在onResume处于连接状态,因此在用户打开应用程序后会立即提示用户访问应用程序,这会令人感到困惑和恐惧。
  2. 如果您拒绝或在同意屏幕上按后退按钮,将再次调用onResume方法并再次显示同意屏幕,从而导致无限循环。
  3. 我宁愿在用户实际需要存储数据时连接api,以便对用户更有意义。我尝试这样做:

    ResultCallback<DriveFolder.DriveFolderResult> folderCreatedCallback = new
            ResultCallback<DriveFolder.DriveFolderResult>() {
                @Override
                public void onResult(@NonNull DriveFolder.DriveFolderResult result) {
                    clearCurrentAction();
    
                    if (!result.getStatus().isSuccess()) {
                        Log.e(TAG, "Error while trying to create the folder");
                        return;
                    }
                    Log.d(TAG, "Created a folder: " + result.getDriveFolder().getDriveId());
                }
            };
    
    public DriveApiHelper(GoogleApiClient mGoogleApiClient) {
        this.mGoogleApiClient = mGoogleApiClient;
    }
    
    public void createBackupsFolder() {
        currentAction = DriveActions.CREATING_FOLDER;
    
        if (mGoogleApiClient.isConnected()) {
            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("test").build();
            Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(
                    mGoogleApiClient, changeSet).setResultCallback(folderCreatedCallback);
        } else {
            mGoogleApiClient.connect();
        }
    }
    

    这就是我的onResumeonConnected方法的样子:

    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            // Create the API client and bind it to an instance variable.
            // We use this instance as the callback for connection and connection
            // failures.
            // Since no account name is passed, the user is prompted to choose.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
    
            mDriveHelper = new DriveApiHelper(mGoogleApiClient);
        }
    
        //Log.d(TAG, mDriveHelper.getCurrentAction() + "");
        Log.d("test", "Connected " + mGoogleApiClient.isConnected());
    }
    
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");
        switch (mDriveHelper.getCurrentAction()) { //resume the last action
            case CREATING_FOLDER:
                mDriveHelper.createBackupsFolder();
                break;
        }
    }
    

    我希望在api被要求连接时保留用户尝试做的事情的参考,我可以在api成功连接后恢复该操作。这是我最接近我需要的实现,但是在同意屏幕上实际点击“允许”按钮后,没有任何api回调被调用(onConnectedonConnectionFailed)。

    我实际上需要再次调用connect方法才能连接并激活onConnected成功恢复用户的操作。

1 个答案:

答案 0 :(得分:0)

原来我忘了覆盖onActivityResult(当时在文档中没有提到它,我不知道它们现在是否包含它)

只需添加:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_RESOLUTION) {
            mGoogleApiClient.connect();
        }
    } else {
        if (requestCode == REQUEST_CODE_RESOLUTION) {
            mDriveHelper.dismissStatusDialog();
        }
    }
}