7

Android アプリケーションでは、

1 つのアクティビティで、 https ://developers.google.com/+/mobile/android/sign-in で説明されているように、Google プラスを使用してサインインでき ます。

しかし、私は別の活動からグーグルプラスからログアウトしたい. ログアウトボタンをクリックすると、このコードが実行されます...しかし、ユーザーが接続されなくなったため、isConnected()メソッドは常にfalseを返します..最初のアクティビティから保存したAccessTokenを使用してユーザーを接続するにはどうすればよいですか?

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

では、アクセストークンを使用して別のアクティビティからユーザーをログアウトするにはどうすればよいですか?

どんな助けでも大歓迎です。

4

1 に答える 1

0

サインインはアプリ全体に対して行われるため、アプリ内のどこからでもサインアウトできます。

サインアウト アクティビティ。

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
    }
  }
}
于 2014-11-25T00:51:21.850 に答える