80

タイトルがすべてを物語っています。カスタムボタンを使用して、ユーザーの Facebook 情報を取得しています (「サインアップ」の目的で)。それでも、私はアプリが最後に登録されたユーザーを記憶したくありません。また、Facebook のネイティブ アプリを介して現在ログインしているユーザーも記憶したくありません。Facebook のログイン アクティビティが毎回ポップアップするようにします。そのため、以前のユーザーをプログラムでログアウトしたいと考えています。

どうやってやるの?これは私がログインを行う方法です:

private void signInWithFacebook() {

    SessionTracker sessionTracker = new SessionTracker(getBaseContext(), new StatusCallback() 
    {
        @Override
        public void call(Session session, SessionState state, Exception exception) { 
        }
    }, null, false);

    String applicationId = Utility.getMetadataApplicationId(getBaseContext());
    mCurrentSession = sessionTracker.getSession();

    if (mCurrentSession == null || mCurrentSession.getState().isClosed()) {
        sessionTracker.setSession(null);
        Session session = new Session.Builder(getBaseContext()).setApplicationId(applicationId).build();
        Session.setActiveSession(session);
        mCurrentSession = session;
    }

    if (!mCurrentSession.isOpened()) {
        Session.OpenRequest openRequest = null;
        openRequest = new Session.OpenRequest(RegisterActivity.this);

        if (openRequest != null) {
            openRequest.setPermissions(null);
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);

            mCurrentSession.openForRead(openRequest);
        }
    }else {
        Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
              @Override
              public void onCompleted(GraphUser user, Response response) {
                  fillProfileWithFacebook( user );
              }
            });
    }
}

理想的には、このメソッドの最初に呼び出しを行い、以前のユーザーをログアウトします。

4

9 に答える 9

161

最新の SDK の更新:

@zeuter の回答は、Facebook SDK v4.7+ に対して正しいものになりました。

LoginManager.getInstance().logOut();

元の答え:

SessionTracker は使用しないでください。これは内部 (パッケージ プライベート) クラスであり、パブリック API の一部として使用されることを意図していません。そのため、その API は、下位互換性を保証することなくいつでも変更される可能性があります。コード内の SessionTracker のすべてのインスタンスを取り除き、代わりにアクティブなセッションを使用できるはずです。

あなたの質問に答えるには、セッション データを保持したくない場合は、アプリを閉じるときにcloseAndClearTokenInformationを呼び出すだけです。

于 2013-01-14T23:05:06.760 に答える
27

これは、Facebookからプログラムでログアウトできるようにするスニペットです。改善すべき点があれば教えてください。

private void logout(){
    // clear any user information
    mApp.clearUserPrefs();
    // find the active session which can only be facebook in my app
    Session session = Session.getActiveSession();
    // run the closeAndClearTokenInformation which does the following
    // DOCS : Closes the local in-memory Session object and clears any persistent 
    // cache related to the Session.
    session.closeAndClearTokenInformation();
    // return the user to the login screen
    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
    // make sure the user can not access the page after he/she is logged out
    // clear the activity stack
    finish();
}
于 2013-03-20T06:26:47.627 に答える
8

SDK 4.0 でセッションクラスが削除されました。ログイン管理は、クラスLoginManagerを通じて行われます。そう:

mLoginManager = LoginManager.getInstance();
mLoginManager.logOut();

参考資料Upgrading to SDK 4.0 には次のように書かれています。

セッションの削除- AccessToken、LoginManager、および CallbackManager クラスは、Session クラスの機能に取って代わります。

于 2015-04-29T20:43:17.480 に答える