简体   繁体   中英

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

In Android Application,

In one activity, I can sign in using google plus as described here : 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?

 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.

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 .

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
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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