You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

一加6设备Android 10系统下选择Google账号后登录界面无限加载问题技术问询

解决Google登录选择账号后无限加载的问题

结合你的设备环境、代码和日志里的NEED_REMOTE_CONSENT提示,问题主要出在混用新旧Google登录API和不必要的账号重置操作干扰了登录流程。下面是一步步的修复方案:

1. 弃用旧API,统一使用新版GoogleSignInClient

Google早已推荐用GoogleSignInClient替代老旧的GoogleApiClient,你代码里同时初始化两者,还调用blockingConnect、clearDefaultAccountAndReconnect这类旧方法,会打乱登录的正常流程。

修改后的初始化代码应该只保留新版API:

// 配置登录选项
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestIdToken(Constant.serverclientid)
    .requestScopes(new Scope(GmailScopes.MAIL_GOOGLE_COM))
    .requestEmail()
    .build();

// 只初始化新版GoogleSignInClient,上下文用当前Activity即可,不用ApplicationContext
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

2. 删除不必要的账号重置操作

你代码里的clearDefaultAccountAndReconnect和revokeAccess会强制清除已有的账号授权状态,导致登录时需要重新请求远程同意,但你的流程没有正确处理这个状态,进而引发无限加载。如果没有特殊的账号重置需求,直接删掉这些代码:

// 移除以下干扰流程的代码:
// ConnectionResult c = gApiClient.blockingConnect();
// if (c.isSuccess() && gApiClient.isConnected()) {
//     Log.e(TAG,"connectionResult = success");
//     gApiClient.clearDefaultAccountAndReconnect().await();
// } else {
//     Log.e(TAG,"connectionResult is failure");
// }
// mGoogleSignInClient.revokeAccess();

3. 用新版API发起登录Intent

替换旧的Auth.GoogleSignInApi.getSignInIntent,改用GoogleSignInClient的方法获取登录Intent:

// 正确获取登录Intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
runOnUiThread(() -> {
    Log.d(TAG,"request account picker");
    Aloading_spinner.setVisibility(View.GONE);
    startActivityForResult(signInIntent, REQUEST_ACCOUNT_PICKER);
});

4. 处理NEED_REMOTE_CONSENT状态(如果需要敏感权限)

因为你请求了GmailScopes.MAIL_GOOGLE_COM这个敏感权限,当遇到NEED_REMOTE_CONSENT时,需要在onActivityResult中正确处理,避免无限加载:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_ACCOUNT_PICKER) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            // 登录成功,这里处理账号数据即可
        } catch (ApiException e) {
            Log.w(TAG, "登录失败,错误码:" + e.getStatusCode());
            // 处理远程同意需求
            if (e.getStatusCode() == CommonStatusCodes.NEED_REMOTE_CONSENT) {
                // 重新发起登录请求,引导用户完成授权
                Intent consentIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(consentIntent, REQUEST_ACCOUNT_PICKER);
            }
        }
    }
}

额外检查项

  • 确认Google Cloud Console里的OAuth 2.0客户端ID配置正确,包名和SHA-1指纹必须和当前应用完全匹配;
  • 尽量更新Google Play Services到最新稳定版,避免版本兼容问题。

内容的提问来源于stack exchange,提问作者All in One King Official

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:52:43