2

当我在 Android 应用程序中使用 Facebook 登录时,会在显示权限弹出窗口之前调用回调。在我的代码中,预期的行为应该是:

  1. 用户打开应用
  2. 用户点击“登录”按钮
  3. 弹出FB权限对话框
  4. 用户接受 FB 权限
  5. “嗨!” 显示吐司消息

但是,实际流程是:

  1. 用户打开应用
  2. 用户点击“登录”按钮
  3. “嗨!” 显示吐司消息
  4. 弹出FB权限对话框

我基本上使用了这个答案中的代码,并添加了一个按钮来在用户点击它时执行登录,而不是在创建活动时执行登录:

public class MainActivity extends Activity implements StatusCallback {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void signIn(View view) {
    OpenRequest open = new OpenRequest(this);
    open.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
    open.setPermissions(Arrays.asList(new String[] { "email", "user_hometown" }));
    open.setCallback(this);
    Session s = new Session(this);
    s.openForRead(open);
  }

  @Override
  public void call(Session session, SessionState state, Exception exception) {
    CharSequence text = "Hi there!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(getApplicationContext(), text, duration);
    toast.show();
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
      Session.getActiveSession().onActivityResult(this, requestCode,
          resultCode, data);
  }
}

谢谢

4

1 回答 1

0

您必须检查何时SessionState为您提供正确的价值。

public void call(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        if (exception == null) {
            //Success
        } else if (state == SessionState.CLOSED_LOGIN_FAILED) {
            //Login fail
        } else if (state.isClosed()) {
            //Logged out...
        }
    }
}
于 2013-05-20T10:23:17.630 回答