如何从这段代码获取Facebook的UserData?

时间:2014-05-05 07:45:41

标签: android facebook

public class MainActivity extends Activity {
private GraphUser user;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setupFacebookConnect(savedInstanceState);
    Button facebook_connect = (Button) findViewById(R.id.loginButton);

    facebook_connect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (isFacebookConnected()) {
                disConnectFacebook();
            } else {
                connectFacebook();
                getProfileInformation(user);
            }

        }
    });
}

private Session.StatusCallback statusCallback = new FBSessionStatus();

public void setupFacebookConnect(Bundle savedInstanceState) {
    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));
        }
    }
}

public boolean isFacebookConnected() {
    Session session = Session.getActiveSession();
    return (session.isOpened()) ? true : false;
}

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

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

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

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode,
            resultCode, data);
}

public class FBSessionStatus implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {

    }
}

public void disConnectFacebook() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
}

这是我获取信息的代码:

public void getProfileInformation(GraphUser user) {
    Session session = Session.getActiveSession();

    boolean enableButtons = (session != null && session.isOpened());
    MainActivity.this.user = user;
    TextView showName = (TextView) findViewById(R.id.showName);
    if (enableButtons && user != null) {

        showName.setText("Hello, " + user.getLastName());
    } else {

        showName.setText("Connect to sign up (or log in)");
    }                       
}
}              

当我运行代码时,它只显示“连接到注册(或登录)”选项。 我不知道我错过了哪里获取用户信息? 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

好吧,我终于完成了这个。

 if (enableButtons && user != null) {

    showName.setText("Hello, " + user.getLastName());

    Intent i = new Intent(this, NextPage.class);
    i.putExtra("name",user.getName());

} else {

    showName.setText("Connect to sign up (or log in)");
}