我想在一个应用程序上实现对facebook基本信息的完整管理,所以请求个人信息、朋友列表和在墙上发帖。所有这些都应该在不使用片段的情况下使用 API 3.0 完成。
现在我被困在我唯一能做到这一点的例子上,那就是 SessionLoginSampleActivity。我现在的程序只不过是那个例子,根据我的布局改编,你可以在帖子的末尾找到它。不幸的是,这个例子并没有超越身份验证,我无法意识到如何与 facebook 进行异步对话并检索用户数据和所有这些东西。
在某个地方有一个完整的例子吗?
认证后如何异步获取用户头像、姓名、好友列表,不分片?我应该在哪里写?我应该创建一个新的侦听器吗?
这个问题可能看起来很奇怪而且很基本,但我真的对 facebook API 很着迷,它看起来如此强大,但对我来说却非常模糊。
请不要回答我:“使用碎片!” :)
非常感谢!
public class FbActivity extends Activity {
private Button buttonLoginLogout, b_friends, b_challenge;
private TextView t_instructions, t_username;
private ProfilePictureView profilePictureView;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
public FbActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_activity);
findAll();
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 findAll() {
buttonLoginLogout = (Button)findViewById(R.id.buttonLoginLogout);
t_instructions = (TextView)findViewById(R.id.instructionsOrLink);
b_friends = (Button)findViewById(R.id.buttonFindFriend);
b_challenge = (Button)findViewById(R.id.buttonFindNewChallenge);
profilePictureView = (ProfilePictureView)findViewById(R.id.profilePicture);
t_username = (TextView)findViewById(R.id.textViewUserName);
}
private void setVisibility(boolean opened) {
if (opened) {
buttonLoginLogout.setText(R.string.logout);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogout(); }
});
t_instructions.setVisibility(TextView.INVISIBLE);
b_friends.setVisibility(Button.VISIBLE);
b_challenge.setVisibility(Button.VISIBLE);
t_username.setVisibility(TextView.VISIBLE);
profilePictureView.setVisibility(ProfilePictureView.VISIBLE);
} else {
buttonLoginLogout.setText(R.string.login);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
t_instructions.setVisibility(TextView.VISIBLE);
b_friends.setVisibility(Button.INVISIBLE);
b_challenge.setVisibility(Button.INVISIBLE);
t_username.setVisibility(TextView.VISIBLE);
profilePictureView.setVisibility(ProfilePictureView.INVISIBLE);
}
}
private void updateView() {
Session session = Session.getActiveSession();
setVisibility(session.isOpened());
}
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();
}
}
}