How to add a logout callback for facebook sdk in android

后端 未结 3 1944
春和景丽
春和景丽 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() {
            @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();
    

提交回复
热议问题