代码之家  ›  专栏  ›  技术社区  ›  Rohan Patel

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

  •  0
  • Rohan Patel  · 技术社区  · 8 年前

    我已经集成了谷歌登录。点击登录按钮,“选择帐户”对话框打开。如果我选择“已创建谷歌登录账户”,则在onActivityResult()中获得回调,但在创建新账户时,从未获得回调。

    代码在这里。

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

    谷歌签名客户端=谷歌签名。getClient(这是谷歌签名);

    处理回拨:

    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 回复  |  直到 8 年前
        1
  •  0
  •   CertainPerformance    5 年前

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

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

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

    Intent signInIntent = loginHandler.mGoogleSignInClient.getSignInIntent();
            activity.startActivityForResult(signInIntent, RC_GET_TOKEN);
    

    在调用上述代码之前,我会将当前已注册的谷歌账户计数。i、 在我的设备中注册的E3帐户。

    一旦用户完成添加新帐户的过程,onStart()方法将得到调用,在这里,我通过以下代码再次检查了google帐户计数:

    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
    

    }

    如果新计数为>最后一次计数,意味着新帐户已注册并重定向到主屏幕。