如何在一个活动中登录和退出Google

时间:2019-02-10 11:36:49

标签: android google-oauth

我是Java和Android编程的初学者。我正在尝试在我的应用上使用Google创建登录系统。我要做的是在活动中切换“登录”和“退出”按钮。但是,一旦成功登录,我将永远无法从Google注销。即使我卸载了该应用程序,也无法注销。

这是我的代码。

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

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    mSignInButton = findViewById(R.id.sign_in_button);
    mSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signIn();
        }
    });

    mSignOutButton = findViewById(R.id.sign_out_button);
    mSignOutButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signOut();
        }
    });


@Override
protected void onStart() {
    // Check for existing Google Sign In account, if the user is already signed in
    // the GoogleSignInAccount will be non-null.
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    updateUI(account);
    super.onStart();
}


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

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}


private void signIn() {
    Intent intent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}


private void signOut() {
    mGoogleSignInClient.signOut();
}


private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        String idToken = account.getIdToken();

        // Send the token to the API endpoint and validate


        // Signed in successfully, show authenticated UI.
        updateUI(account);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        updateUI(null);
    }
}


private void updateUI(GoogleSignInAccount signInAccount) {
    if (signInAccount != null) {
        mSignInButton.setVisibility(View.GONE);
        mSignOutButton.setVisibility(View.VISIBLE);
    } else {
        mSignInButton.setVisibility(View.VISIBLE);
        mSignOutButton.setVisibility(View.GONE);
    }
}

如何在一项活动中正确注销并更新UI?

1 个答案:

答案 0 :(得分:0)

尝试根据docs上有关如何注销和断开连接的代码来更新代码。 这将一直等到用户从服务器注销并更新应用程序UI。

private void signOut() {
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    updateUI(null);
                }
            });
}