How to add a logout callback for facebook sdk in android

后端 未结 3 1931
春和景丽
春和景丽 2021-01-04 06:13

I have integrated Facebook sdk in my android app. As described in the manual I added the login callback for facebook. But I have to change the UI if the user logs out from f

相关标签:
3条回答
  • 2021-01-04 06:31

    Thank you Stan. You helped me solve, but took me some time. To help other people, that's whole code:

    Profile fbProfile = Profile.getCurrentProfile();
    AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
                                                   AccessToken currentAccessToken) {
            if (currentAccessToken == null) {
                Log.d(TAG, "onLogout catched");
                deleteContact();//This is my code
            }
        }
    };
    if (fbProfile == null) {
        Log.d(TAG, "NOT logged in");
        callbackManager = CallbackManager.Factory.create();
    
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");
    
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "onSuccess login Facebook");
                GraphRequest request = GraphRequest.newMeRequest(
                        AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                FacebookSdk.setIsDebugEnabled(true);
                                FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    
                                Log.d(TAG, "AccessToken.getCurrentAccessToken() " + AccessToken.getCurrentAccessToken().toString());
                                Profile profile = Profile.getCurrentProfile();
                                Log.d(TAG, "Current profile: " + profile);
                                if (profile != null) {
                                    Log.d(TAG, String.format("id = %s; name = %s; lastName = %s; uri = %s",
                                            profile.getId(), profile.getFirstName(),
                                            profile.getLastName(), profile.getProfilePictureUri(50, 60)));
                                    name = String.format("%s %s",profile.getFirstName(),profile.getLastName());
                                    fbid = profile.getId();
                                    pushNewContact();//This is my code
                                }
                            }
                        });
                request.executeAsync();
            }
    
            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel");
            }
    
            @Override
            public void onError(FacebookException e) {
                Log.e(TAG, "onError", e);
            }
        });
    } else {
        Log.d(TAG, "Logged with " + fbProfile.getName());
        fbid = fbProfile.getId();
    }
    accessTokenTracker.startTracking();
    
    0 讨论(0)
  • 2021-01-04 06:37

    there are 2 possible ways:

    1) you need to overwrite in on create AccessTokenTracker like this:

    accessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
                                                           AccessToken currentAccessToken) {
                        if (currentAccessToken == null) {
                            //write your code here what to do when user logout
                        } 
                    }
                }
    

    2) You can call LoginManager.logOut() to log out the user

    hope this will help you :)

    0 讨论(0)
  • 2021-01-04 06:39

    This worked for me:-

    profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(
                    Profile oldProfile,
                    Profile currentProfile) {
    
                if(currentProfile == null){
                    tvUNameandEmail.setText(R.string.app_name);
                }
            }
        };
    
    0 讨论(0)
提交回复
热议问题