[英]How to sign out a user from a different activity with Google Sign-in?
在我的登录活动中,我正在使用 Google Sign-in 登录用户。 我希望注销按钮处于不同的活动中,但我不确定如何实现这一点。 我在网上寻找了解决方案,但他们都使用了一个已弃用的库。
这是我的登录活动中的代码:
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);
}
}
}
仅供参考。 这就是我从 LoginActivity 以外的活动(HomeActivity)注销用户所做的工作。 你需要做的是调用signOut()
从两种方法onCreate()
或onStart()
方法中LoginActivty从另一个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();
}
...
...
}
家庭活动
private void logoutUser() {
session.setLogin(false);
// Launching the login activity
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
希望能帮助到你。
在要注销的活动中,您可以执行以下操作:
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( signOut()
和revokeAccess()
的定义。
重要的是,在这两个活动中,您将应用程序上下文而不是活动上下文传递给GoogleSignIn.getClient()
。
尝试这个
使用下面的代码并清除所有保存的数据
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.