使用Facebook SDK登录后无限循环

时间:2014-11-27 14:14:50

标签: android facebook android-facebook

我已经按照facebook开发人员的指导,将我的Android应用程序与facebook用户的帐户连接起来。 这是我的代码,非常接近教程代码:

private UiLifecycleHelper uiHelper;
private LoginButton login_button;

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

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.facebook_login_fragment,
            container, false);

    login_button = (LoginButton) view.findViewById(R.id.login_facebook);
    login_button.setFragment(this);
    // permissions
    // login_button.setReadPermissions(Arrays.asList("publish_actions"));

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();

    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
    // TODO success

    Session session = Session.getActiveSession();
    if (session != null && !session.isClosed()) {
        Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                this, "publish_actions");
        session.requestNewPublishPermissions(newPermissionsRequest);
    }

}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        Log.i("FACEBOOK", "Logged in...");

    } else if (state.isClosed()) {
        Log.i("FACEBOOK", "Logged out...");
    }
}

但登录后,我的代码在onSessionStateChange函数内部进行无限循环(注销时此问题不会出现)。怎么了?

2 个答案:

答案 0 :(得分:0)

尝试删除" // TODO成功"在onActivityResult中,执行onSessionStateChange中的所有内容

答案 1 :(得分:0)

您无法在登录过程中申请任何权限。 一旦登录过程完成,那么只在另一个事件中请求权限(比如按下其他按钮)。