简体   繁体   English

如何使用google plus的accessToken从不同的活动中注销android中的用户

[英]How to use accessToken of google plus to sign out user in android from different activity

In Android Application, 在Android应用程序中,

In one activity, I can sign in using google plus as described here : https://developers.google.com/+/mobile/android/sign-in 在一项活动中,我可以使用google plus登录,如下所述: https//developers.google.com/+/mobile/android/sign-in

But i want to do log out from google plus from different activity. 但我想从谷歌加上从不同的活动登出。 So When I click on Log out button then i am executing this code...But here isConnected() method always return false because user is no longer connected..So how can i connect user using AccessToken Which i store from first activity? 所以,当我点击退出按钮然后我正在执行此代码...但是这里isConnected()方法总是返回false,因为用户不再连接..那么我如何使用AccessToken连接用户我从第一个活动存储?

 if (mPlusClient.isConnected()) {
        mPlusClient.clearDefaultAccount();
        mPlusClient.disconnect();
        Log.d(TAG, "User is disconnected.");
    }  

So how can i use access token to log out user from different activity ? 那么我如何使用访问令牌从不同的活动中注销用户?

Any help will be appreciated. 任何帮助将不胜感激。

The signin is for the entire app so you can sign out anywhere in the app. 登录适用于整个应用,因此您可以在应用中的任何位置注销。

Signout activity. 注销活动。

Initialize the GoogleApiClient object in your Activity.onCreate handler. 在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();
}

Invoke GoogleApiClient.connect during Activity.onStart . 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM