gpt4 book ai didi

android - 如何使用 Google 登录从不同的 Activity 中注销用户?

转载 作者:行者123 更新时间:2023-11-29 23:22:23 25 4
gpt4 key购买 nike

在我的登录 Activity 中,我使用 Google 登录让用户登录。我希望注销按钮处于不同的 Activity 中,但我不确定如何实现它。我在网上寻找解决方案,但所有解决方案都使用已弃用的库。

这是我的登录 Activity 中的代码:

public class LoginActivity extends AppCompatActivity {

@BindView(R.id.sign_in_button) SignInButton signInButton;

public GoogleSignInClient mGoogleSignInClient;
private int RC_SIGN_IN;
private static final String TAG = "Login Activity Error";
private static GoogleSignInOptions gso;


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

ButterKnife.bind(this);
signInButton.setSize(SignInButton.SIZE_STANDARD);

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

mGoogleSignInClient = GoogleSignIn.getClient(getApplicationContext(), gso);

findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);

}
});
}

@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}

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

if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}

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

updateUI(account);
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}

private void updateUI(GoogleSignInAccount account) {
if (account != null) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra(MainActivity.PERSON_NAME, account.getDisplayName());
intent.putExtra(MainActivity.PERSON_FAMILY_NAME, account.getGivenName());
intent.putExtra(MainActivity.PERSON_GIVEN_NAME, account.getFamilyName());
intent.putExtra(MainActivity.PERSON_EMAIL, account.getEmail());
intent.putExtra(MainActivity.PERSON_ID, account.getId());
intent.putExtra(MainActivity.PERSON_PHOTO, account.getPhotoUrl());
startActivity(intent);
}
}
}

Google 说要使用此代码退出,但我不确定如何更改它以在不同的 Activity 中工作:

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

非常感谢任何帮助。谢谢!

最佳答案

在你想要注销的 Activity 中,你可以做这样的事情:

findViewById(R.id.logout_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(getApplicationContext(), GoogleSignInOptions.DEFAULT_SIGN_IN);
signOut(googleSignInClient);
revokeAccess(googleSignInClient);
}
});

可以找到signOut()revokeAccess() 的定义here .

重要的是,在这两个 Activity 中,您将应用程序上下文而不是 Activity 上下文传递给 GoogleSignIn.getClient()

关于android - 如何使用 Google 登录从不同的 Activity 中注销用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com