2

我有一个 buttonLoginLogout Button,它是一个 facebook 登录按钮(我将 facebook sdk 导入到我的项目中),这是我在 mainActivity 中的方法:

它取自 Facebook 活动的 LoginUsingActivityActivity 示例,我没有看到 GraphUser 或未完成的方法,所以我怎样才能获得通过 facebook 连接的用户详细信息...

我尝试添加“void onCompleted”方法(最后如您所见),但是我无法覆盖它并且我无法获取用户详细信息,并且当我登录到 Facebook(并且登录成功)时,它没有到达 oncompleted方法...

如何获取成功登录 facebook 的 GraphUser?

另外,我在这里添加了 onCompleted 方法,它不在这里,它说它没有覆盖另一个方法..

多谢

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()) {
        //button logout string
        buttonLoginLogout.setOnClickListener(new OnClickListener() {
            public void onClick(View view) { onClickLogout(); }
        });
    } else {
        //button log in string
        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();
    }

    //doesnt get here, why?????????
    public void onCompleted(GraphUser user, Response response) {


    }
}
4

1 回答 1

8

把它写在一个方法中:

final Session session = Session.getActiveSession();
    if (session != null && session.isOpened()) {
        // If the session is open, make an API call to get user data
        // and define a new callback to handle the response
        Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                // If the response is successful
                if (session == Session.getActiveSession()) {
                    if (user != null) {
                        user_ID = user.getId();//user id
                        profileName = user.getName();//user's profile name
                        userNameView.setText(user.getName());
                    }   
                }   
            }   
        }); 
        Request.executeBatchAsync(request);
    }  

并确保仅在成功登录后调用此方法。这意味着你应该从onActivityResult.

于 2013-03-05T00:00:48.597 回答