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

Android应用Google SignIn添加账户无响应,返回RESULT_CANCELED问题求助

Fixing Google Sign-In "Add Account" Freeze & RESULT_CANCELED Issue

Hey there! Let's work through this frustrating Google Sign-In problem you're hitting. That screen blur and unresponsive state followed by a RESULT_CANCELED (code 0) when trying to add a new account is definitely a roadblock—here are the most likely fixes to get things working:

1. Double-Check Your GoogleSignInOptions Configuration

The most common culprit here is missing a setting that allows account selection. When you want to add a new account, don't pre-set an account name in your GoogleSignInOptions—instead, explicitly enable account picking:

// Correct configuration for allowing new account selection
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail() // Or whatever scopes your app needs
        .setAllowPickAccount(true) // Critical: Lets users pick/add new accounts
        .build();

If you previously used setAccountName(someExistingAccount), that forces the app to stick to a single account, which breaks the "add account" flow entirely.

2. Verify GoogleApiClient Setup & Auto-Management

Make sure your GoogleApiClient is properly built with lifecycle auto-management and includes the Sign-In API:

private GoogleApiClient mGoogleApiClient;
private SignInButton signInButton;
private static final int RC_SIGN_IN = 1001;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    signInButton = findViewById(R.id.sign_in_button);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .setAllowPickAccount(true)
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this) // Second param is your OnConnectionFailedListener
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    // Attach click listener to your "Add Account" button
    signInButton.setOnClickListener(v -> launchAddAccountFlow());
}

private void launchAddAccountFlow() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

3. Fix Manifest & OAuth Client ID Configuration

Don't overlook these critical setup steps that often cause silent failures:

  • Add the Google Play Services meta-data to your AndroidManifest.xml:
    <application ...>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>
    
  • Ensure your OAuth 2.0 Client ID (from Google Cloud Console) is correctly configured:
    • It must be an Android-type client ID.
    • The package name and SHA-1 fingerprint must exactly match your app's setup. A mismatch here frequently leads to canceled results or unresponsive screens.

4. Properly Handle Activity Results

Make sure you're correctly processing the result in onActivityResult, and handle failures gracefully:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // Success! Retrieve the new account
            GoogleSignInAccount newAccount = result.getSignInAccount();
            // Process the account (e.g., save to your app's user system)
        } else {
            // Handle RESULT_CANCELED or other failures
            Log.d("SignInDebug", "Sign-in canceled: " + result.getStatus().getStatusMessage());
            // Reset the client to clear cached account state
            if (mGoogleApiClient.isConnected()) {
                mGoogleApiClient.clearDefaultAccountAndReconnect();
            }
        }
    }
}

5. Update Google Play Services

Outdated Google Play Services on the device can cause compatibility issues. Ask users to update Google Play Services to the latest version, and ensure your project's play-services-auth dependency uses a stable, up-to-date version in build.gradle:

dependencies {
    implementation 'com.google.android.gms:play-services-auth:20.7.0' // Use the latest stable version
}

内容的提问来源于stack exchange,提问作者Ajeet Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:07:53