【问题标题】:How to use accessToken of google plus to sign out user in android from different activity如何使用google plus的accessToken从不同的活动中注销android中的用户
【发布时间】:2013-07-07 01:05:52
【问题描述】:
在 Android 应用程序中,
在一项活动中,我可以使用 google plus 登录,如下所述:
https://developers.google.com/+/mobile/android/sign-in
但我想从谷歌和其他活动中注销。
因此,当我单击注销按钮时,我正在执行此代码...但是这里 isConnected() 方法总是返回 false,因为用户不再连接..那么我如何使用我从第一个活动中存储的 AccessToken 连接用户?
if (mPlusClient.isConnected()) {
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
Log.d(TAG, "User is disconnected.");
}
那么我如何使用访问令牌从不同的活动中注销用户?
任何帮助将不胜感激。
【问题讨论】:
标签:
android
logout
access-token
【解决方案1】:
登录适用于整个应用,因此您可以在应用中的任何位置退出。
退出活动。
在您的 Activity.onCreate 处理程序中初始化 GoogleApiClient 对象。
private GoogleApiClient mGoogleApiClient;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
在 Activity.onStart 期间调用 GoogleApiClient.connect。
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
//process sign out in click of button.
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_out_button) {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect(); //may not be needed
}
}
}