gpt4 book ai didi

android - 如何退出谷歌认证?

转载 作者:行者123 更新时间:2023-12-02 01:39:04 25 4
gpt4 key购买 nike

我在我的应用程序中集成了 Google 身份验证登录,但登录后,如果我注销我的帐户,每次应用程序都会自动登录旧用户帐户。

主要 Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private int RC_SIGN_IN=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account != null){
String personName = account.getDisplayName();
String personGivenName = account.getGivenName();
String personFamilyName = account.getFamilyName();
String personEmail = account.getEmail();
String personId = account.getId();
Uri personPhoto = account.getPhotoUrl();
Intent i=new Intent(MainActivity.this,Welcome.class);
i.putExtra("pn",personName);
i.putExtra("pe",personEmail);

startActivity(i);
}
findViewById(R.id.sign_in_button).setOnClickListener(this);


}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
// ...
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@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 handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);

// Signed in successfully, show authenticated UI.
Intent i=new Intent(MainActivity.this,Welcome.class);
startActivity(i);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w("SignInFailed", "signInResult:failed code=" + e.getStatusCode());
Toast.makeText(MainActivity.this,"SignInFailed",Toast.LENGTH_SHORT).show();
}
}
}

欢迎 Activity

public class Welcome extends AppCompatActivity {
TextView textView,textView2;
GoogleSignInClient mGoogleSignInClient;
private GoogleApiClient mGoogleApiClient;

private FirebaseAuth mAuth;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
textView=findViewById(R.id.textView);
textView2=findViewById(R.id.textView2);
button=findViewById(R.id.button);

Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j =(String) b.get("pn");
textView.setText(j);
String k =(String) b.get("pe");
textView2.setText(k);
}
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if(account == null){
Intent i=new Intent(Welcome.this,MainActivity.class);
startActivity(i);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


signOut();

}
});

}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Intent i=new Intent(Welcome.this,MainActivity.class);
startActivity(i);
}
});
}
//
}

我已经使用了有关注销的谷歌文档,但无法解决我的问题。我还没有从其他人已经提出的问题中发现任何有用的问题。我感谢你们提供的任何帮助。

最佳答案

您还需要从 GoogleSignInClientFirebaseAuth 当前用户注销,如下所示:

 //sign out of the user and start login activity.
public void signOut() {
signOutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(getContext(), gso);
mGoogleSignInClient.signOut();
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getContext(), LoginActivity.class));
}
});
}

关于android - 如何退出谷歌认证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499717/

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