Android Facebook SDK: Check if the user is logged in or not

后端 未结 10 1464
南旧
南旧 2020-12-04 12:43

I\'m have a feature on my Android app where the user authorizes the app and shares a link.

I also need to give an option for the user to logout of facebook and I nee

相关标签:
10条回答
  • 2020-12-04 12:54

    I had the same issue. Here is my solution using SDK 4.0:

    First of all, in your activity dealing with login check, be sure to call this primary:

            FacebookSdk.sdkInitialize(this.getApplicationContext());
    

    In your onCreate method put this :

    updateWithToken(AccessToken.getCurrentAccessToken());
    
        new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
                updateWithToken(newAccessToken, handler);
            }
        };
    

    Then add the method called:

    private void updateWithToken(AccessToken currentAccessToken) {
        if (currentAccessToken != null) {
            fillUIWithFacebookInfos(handler);
        } else {
            login();
        }
    }
    

    This way will handle the already logged in user and the newly logged in user.

    0 讨论(0)
  • 2020-12-04 12:57

    This seems to be working quite well with the new sdk.

    private boolean isFacebookLoggedIn(){
        Session session = Session.getActiveSession();
    
        if (session != null) {
            //Session can be open, check for valid token
            if (!session.isClosed()) {
                if(!session.getAccessToken().equalsIgnoreCase("")){
                    return true;
                }
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-04 12:59

    @diljeet was right. https://stackoverflow.com/a/29375963/859330

    In addition, use

    return AccessToken.getAccessToken() != null && Profile.getCurrentProfile()!=null;

    It always works this way.

    0 讨论(0)
  • 2020-12-04 13:04

    Facebook SDK 4.x versions have a different method now:

    boolean loggedIn = AccessToken.getCurrentAccessToken() != null;
    

    or

    by using functions

    boolean loggedIn;
    //...
    loggedIn = isFacebookLoggedIn();
    //...
    public boolean isFacebookLoggedIn(){
        return AccessToken.getCurrentAccessToken() != null;
    }
    

    Check this link for better reference https://developers.facebook.com/docs/facebook-login/android check this heading too "Access Tokens and Profiles" it says "You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile()

    0 讨论(0)
  • I struggled to find a simple answer to this in the FB docs. Using the Facebook SDK version 3.0 I think there are two ways to check if a user is logged in.

    1) Use Session.isOpened()

    To use this method you need to retrieve the active session with getActiveSession() and then (here's the confusing part) decipher if the session is in a state where the user is logged in or not. I think the only thing that matters for a logged in user is if the session isOpened(). So if the session is not null and it is open then the user is logged in. In all other cases the user is logged out (keep in mind Session can have states other than opened and closed).

    public boolean isLoggedIn() {
        Session session = Session.getActiveSession();
        return (session != null && session.isOpened());
    }
    

    There's another way to write this function, detailed in this answer, but I'm not sure which approach is more clear or "best practice".

    2) Constantly monitor status changes with Session.StatusCallback and UiLifecycleHelper

    If you follow this tutorial you'll setup the UiLifecycleHelper and register a Session.StatusCallback object with it upon instantiation. There's a callback method, call(), which you override in Session.StatusCallback which will supposedly be called anytime the user logs in/out. Within that method maybe you can keep track of whether the user is logged in or not. Maybe something like this:

    private boolean isLoggedIn = false; // by default assume not logged in
    
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (state.isOpened()) { //note: I think session.isOpened() is the same
                isLoggedIn = true;
            } else if (state.isClosed()) {
                isLoggedIn = false;
            }
        }
    };
    
    public boolean isLoggedIn() {
        return isLoggedIn;
    }
    

    I think method 1 is simpler and probably the better choice.

    As a side note can anyone shed light on why the tutorial likes to call state.isOpened() instead of session.isOpened() since both seem to be interchangeable (session.isOpened() seems to just call through to the state version anyway).

    0 讨论(0)
  • 2020-12-04 13:11
    Session.getActiveSession().isOpened()
    

    returns true if user is logged in, false if not

    0 讨论(0)
提交回复
热议问题