I have this code from the Facebook Developers site. What it does is simply allow the user to log in and log out. What I want to do now is check whether they are logged in or out in another activity. So, they log in, go to a new activity, and in that new activity, I want to see if they are logged in. If so, I'll display their profile picture and name. So what I need to know is:
Thanks!
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);
}
}
I highly suggest you to take a look at https://github.com/sromku/android-simple-facebook for Facebook on Android. All the session part will be handled directly by the library.
Try this, I can not grant that it is correct, but I've used in my projects.
//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));
}
}
.....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.