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

后端 未结 10 1479
南旧
南旧 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.

提交回复
热议问题