0

我只是从 facebook api 开始,所以如果我的问题很容易解决,请不要讨厌我的提问。我正在开发一个登录 FB 的 Android 应用程序。我在 devFB 教程中设置了 FB api。一切正常,您按下登录按钮,FB 登录屏幕显示为弹出窗口,您可以在其中登录等。但是当设备上有一个 FBapp 时,我的代码将重定向到该应用程序进行授权,然后什么也不做,零,空。

        Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(final Session session, SessionState state, Exception exception) {
            // make request to the /me API
            if (session.isOpened()) {
                // make request to the /me API
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                // callback after Graph API response with user object
                  @Override
                  public void onCompleted(GraphUser user, Response response) {
                      if (user != null) {
                                    TextView welcome = (TextView) findViewById(R.id.instructionsOrLink);
                                    welcome.setText("Hello "+ user.getName() + "!");

它是教程代码的副本。

有什么建议可以防止这种情况吗?

4

1 回答 1

1

我正在使用此代码,也许您应该尝试一下:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(final Session session, SessionState state, Exception exception) {

            if (session.isOpened()) {

                // make request to the /me API
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {

                            emailAddress = user.getProperty("email").toString();
                            name = user.getName();
                            fbId = user.getId();
                            Communication.loginWithFb(FacebookLoginActivity.this, user.getId());

                        }
                    }
                });
            } else if (session.getState().equals(SessionState.CLOSED_LOGIN_FAILED)) {

                Toast.makeText(FacebookLoginActivity.this, getResources().getString(R.string.login_with_facebook_error), Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }, null);
}

private Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback,
        List<String> permissions) {
    OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setCallback(callback);
    Session session = new Builder(activity).build();
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}

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

@Override
public void onDataDownloaded(CommunicationOutput<UserData> output) {

    ...
}
于 2013-05-27T13:01:03.653 回答