![](/img/trans.png)
[英]How to sign out a user from a different activity with Google Sign-in?
[英]How to use accessToken of google plus to sign out user in android from different activity
在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.");
}
那么我如何使用訪問令牌從不同的活動中注銷用戶?
任何幫助將不勝感激。
登錄適用於整個應用,因此您可以在應用中的任何位置注銷。
注銷活動。
在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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.