我从Facebook Developers网站获得此代码。它的作用是让用户登录并注销。我现在要做的是检查他们是否登录或退出其他活动。因此,他们登录,转到新活动,在新活动中,我想看看他们是否已登录。如果是,我会显示他们的个人资料图片和名称。所以我需要知道的是:
谢谢!
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(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.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
}).executeAsync();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
答案 0 :(得分:2)
我强烈建议您在Android上查看https://github.com/sromku/android-simple-facebook for Facebook。所有会话部分将由图书馆直接处理。
答案 1 :(得分:1)
试试这个,我不能认为它是正确的,但我已经在我的项目中使用过。
//do logout
public static void logout() {
Session session = Session.getActiveSession();
if (session != null && !session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
//check whether login/out.
public static boolean isLoggedIn() {
Session session = Session.getActiveSession();
return (session != null && session.getAccessToken() != null && session.getAccessToken().length() > 1);
}
//In your create you might do this
......
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(_fragment)
.setCallback(_statusCallback));
}
}
.....