使用GDAA连接到Google云端硬盘时,屏幕变暗一秒钟

时间:2018-11-29 11:32:32

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

正如标题所述,这是连接到Google云端硬盘时发生的唯一问题。它可以成功连接并创建文件,但是每个请求都出现黑屏并不是一件好事。我已经在几种实际设备上对其进行了测试,并获得了相同的结果。这个问题可以解决吗?任何建议表示赞赏。这是我的代码:

mGoogleSignInClient = buildGoogleSignInClient();
startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);

构建Google Drive登录客户端:

  private GoogleSignInClient buildGoogleSignInClient () {
    Log.i(TAG, "signIn build");
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .build();
    return GoogleSignIn.getClient(this, signInOptions);
}

onActivityResult:

 @Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case COMPLETE_AUTHORIZATION_REQUEST_CODE:
            // Called after user is signed in.
            if (resultCode == RESULT_OK) {
                Log.i(TAG, "Signed in successfully.");
                mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this));
                mDriveResourceClient = Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this));

                createDriveFile();
            }
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

经过长时间的搜索,我想到了以下解决方案:

public Task<GoogleSignInAccount> silentSignIn ()

通过使用此方法,用户将以静默方式登录,因此屏幕将保持不变。这是完整的功能代码:

Task<GoogleSignInAccount> task = googleSignInClient.silentSignIn();
if (task.isSuccessful()) {
        GoogleSignInAccount signInAccount = task.getResult();
        Log.i(TAG, "immediate result available (silentSignIn)");
        updateViewWithGoogleSignIn(signInAccount);
    } else {
        // There's no immediate result ready, displays some progress indicator and waits for the
        // async callback.
        task.addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
            @Override
            public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                try {
                    GoogleSignInAccount signInAccount = task.getResult(ApiException.class);
                    updateViewWithGoogleSignIn(signInAccount);
                } catch (ApiException apiException) {
                    // You can get from apiException.getStatusCode() the detailed error code
                    // e.g. GoogleSignInStatusCodes.SIGN_IN_REQUIRED means user needs to take
                    // explicit action to finish sign-in;
                    // Please refer to GoogleSignInStatusCodes Javadoc for details
                    updateButtonsAndStatusFromErrorCode(apiException.getStatusCode());
                }
            }
        });
    }

访问云端硬盘内容并执行必要的任务:

private void updateViewWithGoogleSignIn(GoogleSignInAccount signInAccount) {
    // Build a drive client.
    mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount);
    // Build a drive resource client.
    mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);

    // TODO:
}

有关静默登录的更多信息:documentation