如何使用 Google 登录从不同的活动中退出用户?
Posted
技术标签:
【中文标题】如何使用 Google 登录从不同的活动中退出用户?【英文标题】:How to sign out a user from a different activity with Google Sign-in? 【发布时间】:2019-05-31 14:21:48 【问题描述】:在我的登录活动中,我正在使用 Google 登录用户登录。我希望注销按钮处于不同的活动中,但我不确定如何实现这一点。我在网上寻找过解决方案,但它们都使用了一个已弃用的库。
这是我的登录活动中的代码:
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 说要使用此代码退出,但我不确定如何更改它以在不同的活动中工作:
private void signOut()
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>()
@Override
public void onComplete(@NonNull Task<Void> task)
// ...
);
非常感谢任何帮助。谢谢!
【问题讨论】:
【参考方案1】:仅供参考。这就是我为从除 LoginActivity 之外的活动(HomeActivity)中注销用户所做的工作。您需要做的是在 LoginActivty 中从 onCreate()
或 onStart()
方法调用 signOut()
方法,并在您希望用户退出时从另一个 Activty(在我的情况下为 HomeActivity)启动 LoginActivty。
登录活动
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
...
...
// I've used session to save current user availibility in shared preference to
//check if user is already logged in or not
if (session.isLoggedIn())
// User is already logged in. Take him to main activity
Intent intent = new Intent(LoginActivity.this,
HomeActivity.class);
startActivity(intent);
finish();
else
//This is what you need to do.
signOut();
revokeAccess();
...
...
HomeActivity
private void logoutUser()
session.setLogin(false);
// Launching the login activity
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intent);
finish();
希望对你有帮助。
【讨论】:
【参考方案2】:试试这个
使用下面的代码并清除所有保存的数据
if (mGoogleApiClient.isConnected())
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
【讨论】:
以上是关于如何使用 Google 登录从不同的活动中退出用户?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用google plus的accessToken从不同的活动中注销android中的用户