Facebook Session已经开放

时间:2014-06-10 07:54:52

标签: android android-facebook

我最近更新了facebook sdk,以前工作的代码不再有效。

我收到以下异常:

06-10 10:37:58.146: E/AndroidRuntime(28184): java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
06-10 10:37:58.146: E/AndroidRuntime(28184):    at com.facebook.Session.open(Session.java:985)
06-10 10:37:58.146: E/AndroidRuntime(28184):    at com.facebook.Session.openForRead(Session.java:388)

我有以下代码来打开会话:

        Session session = Session.getActiveSession();
        if (session == null) {
            session = new Session(DealActivity.this);
        }
        //session.isOpened() apparently is false!
        if (!session.isOpened()) {
            Session.OpenRequest openRequest = null;
            openRequest = new Session.OpenRequest(this);
            openRequest.setPermissions(FacebookManager.READ_PERMISSIONS);
            openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            openRequest.setCallback(callback);
            session.openForRead(openRequest);
            return;
        } else {
            getDataFromFacebook();
        }

我也在所有需要的回调中使用facebook UiLifecycleHelper对象(它曾经工作过)。

我的onActivityResult回调我先尝试过这个:

    uiHelper.onActivityResult(requestCode, resultCode, data);

然后是以下代码:

    Session session = Session.getActiveSession();
    if (session != null) {
        session.onActivityResult(DealActivity.this, requestCode, resultCode, data);
    } 

他们两个都没有用。

这是facebook Session.StatusCallback

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (exception != null) {
        exception.printStackTrace();
        return;
    }
    if (state.isOpened()) {
        getDataFromFacebook();
    } else if (state.isClosed()) {
    }
}

我想要实现的目标是:

  1. 登录
  2. 获取有关用户的基本数据(我知道该怎么做)
  3. 注销
  4. 在查看facebook开发者指南后,我找不到任何简单的例子,我会很感激这样的例子。

1 个答案:

答案 0 :(得分:2)

我在这里发布一个简单的类,在Facebook的例子中:

public class LoginUsingActivityActivity extends Activity {
    private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";

    private TextView textInstructionsOrLink;
    private Button buttonLoginLogout;
    private Session.StatusCallback statusCallback = new SessionStatusCallback();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        buttonLoginLogout = (Button)findViewById(R.id.buttonLoginLogout);
        textInstructionsOrLink = (TextView)findViewById(R.id.instructionsOrLink);

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }
        }

        updateView();
    }

    @Override
    public void onStart() {
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    private void updateView() {
        Session session = Session.getActiveSession();
        if (session.isOpened()) {
            textInstructionsOrLink.setText(URL_PREFIX_FRIENDS + session.getAccessToken());
            buttonLoginLogout.setText(R.string.logout);
            buttonLoginLogout.setOnClickListener(new OnClickListener() {
                public void onClick(View view) { onClickLogout(); }
            });
        } else {
            textInstructionsOrLink.setText(R.string.instructions);
            buttonLoginLogout.setText(R.string.login);
            buttonLoginLogout.setOnClickListener(new OnClickListener() {
                public void onClick(View view) { onClickLogin(); }
            });
        }
    }

    private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        } else {
            Session.openActiveSession(this, true, statusCallback);
        }
    }

    private void onClickLogout() {
        Session session = Session.getActiveSession();
        if (!session.isClosed()) {
            session.closeAndClearTokenInformation();
        }
    }

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            updateView();
        }
    }
}

希望它可以帮助你:)