在谷歌登录时,onActivityResult()不会调用创建新帐户

时间:2018-05-18 12:35:40

标签: android google-play-services google-authentication google-login

我已整合谷歌登录。点击登录按钮"选择帐户"对话框正在打开。如果我选择已创建的谷歌帐户进行登录,请在onActivityResult()中获取回调,但在创建新帐户时,永远不会回调。

代码在这里。

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

googleSignInClient = GoogleSignIn.getClient(this,googleSignInOptions);

处理回调:

private void signIn() {
    googleSignInClient.signOut();
    Intent signInIntent = googleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, 123);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 123 && data != null) {
        handleSignInResult(data);
    }
}

private void handleSignInResult(Intent data) {
    Task<GoogleSignInAccount> signInTask = GoogleSignIn.getSignedInAccountFromIntent(data);
    try {
        GoogleSignInAccount googleSignInAccount = signInTask.getResult(ApiException.class);
        if (googleSignInAccount != null) {
            updateUI(googleSignInAccount);
        }
    } catch (ApiException e) {
        e.printStackTrace();
    }
}

不要对此代码有什么问题。这是谷歌问题吗?

摇篮:

 compile 'com.google.firebase:firebase-core:12.0.1'
compile 'com.google.firebase:firebase-auth:12.0.1'
compile 'com.google.firebase:firebase-messaging:12.0.1'
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-auth:12.0.1'

1 个答案:

答案 0 :(得分:0)

在我们从谷歌获得发布更新之前,这是另一个解决方案。

正如我们所知,onActivityResult()仅在用户添加新帐户时才调用,但onRestart()和start()方法被调用。

以下是打开谷歌专注屏幕以获取帐户或添加新帐户的代码。

Intent signInIntent = loginHandler.mGoogleSignInClient.getSignInIntent();         activity.startActivityForResult(signInIntent,RC_GET_TOKEN); 在调用上述代码之前,我保留了当前注册的Google帐号。即在我的设备中注册的3个帐户。

一旦用户完成添加新帐户流程,onStart()方法将获得通话,此处我再次通过以下代码检查谷歌帐号:

Account[] googleAccounts = AccountManager.get(activity).getAccountsByType("com.google");

if (googleAccounts != null && googleAccounts.length > 0) {
    if (lastGoogleAccounts == 0) {
        lastGoogleAccounts = googleAccounts.length;
    } else {
        if (googleAccounts.length > lastGoogleAccounts) {
         // do login

}

如果新计数是&gt;最后一次计数,表示新帐户已注册并重定向到主屏幕。

希望这会对你有所帮助。