简体   繁体   中英

GoogleApiClient onConnected callback doesn't get called correctly

I'm trying to use the DriveApi in order to create some folders and upload a text file with some data for a user.

I've tried implementing the quick-start guide from ( link ), but it has a few fundamental issues:

  1. The api gets connected at onResume so the user will get prompted to give access to the app immediately after he opens the app which is confusing and scary.
  2. If you deny or press the back button at the consent screen, the onResume method will get called again and the consent screen will be shown one more time, leading to an infinite loop.

I would rather like to connect the api when the user actually needs to store data so that will make more sense to the user. I tried doing it like this:

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

and this is how my onResume and onConnected methods look like:

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

I was hoping that keeping a reference of what the user tried to do when the api was asked to connect, I can resume that action after the api successfully connected. This is the closest implementation I've got to fit my needs, but after actually clicking the 'Allow' button from the consent screen none of the api callbacks gets called ( onConnected , onConnectionFailed ).

I actually need to call the connect method one more time in order to get connected and also fire the onConnected successfully resuming the users' action.

Turns out that I forgot about overriding onActivityResult (it wasn't mentioned in the documentation at that time and I don't know if they included it now)

Just add:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM