2015-05-14 16 views
8

Tôi đã tích hợp Facebook sdk trong ứng dụng Android của mình. Như được mô tả trong sách hướng dẫn, tôi đã thêm gọi lại đăng nhập cho facebook. Nhưng tôi phải thay đổi giao diện người dùng nếu người dùng đăng xuất khỏi facebook. Tôi đặt mã đó ở đâu. Mã của tôi để đăng nhập làCách thêm chức năng gọi lại đăng xuất cho facebook sdk trong android

  /** 
     * Login Callback for facebook login 
     */ 
     callbackManager = CallbackManager.Factory.create(); 

    LoginManager.getInstance().registerCallback(callbackManager, 
      new FacebookCallback<LoginResult>() { 

       @Override 
       public void onSuccess(LoginResult loginResult) { 
        //Call updateUI() 

        setData("provider","facebook"); 
        loginType = LoginTypes.FB_LOGIN; 
        isLoggedin = true; 
        GraphRequest request = GraphRequest.newMeRequest(
          loginResult.getAccessToken(), 
          new GraphRequest.GraphJSONObjectCallback() { 
           @Override 
           public void onCompleted(
             JSONObject object, 
             GraphResponse response) { 
            // Application code 

            txtName.setText(response.toString()); 
            updateUI(); 
           } 
          }); 
        Bundle parameters = new Bundle(); 
        parameters.putString("fields", "id,name,email"); 
        request.setParameters(parameters); 
        request.executeAsync(); 
       } 

       @Override 
       public void onCancel() { 
        editText_message.setText("Login Cancelled."); 
        // App code 
       } 

       @Override 
       public void onError(FacebookException exception) { 
        // App code 
       } 
      }); 

Trả lời

30

có 2 cách có thể:

1), bạn cần phải ghi đè lên ở trên tạo AccessTokenTracker như thế này:

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) Bạn có thể gọi LoginManager.logOut() để đăng xuất người dùng

hy vọng điều này sẽ giúp bạn :)

+3

lớn nó làm việc ... Tôi chỉ cần thêm 'accesstokenTracker.startTracking() 'sau khi người dùng đăng nhập – bytestorm

+0

Vui mừng khi giúp)) –

+0

Đây là những gì tôi đang tìm kiếm :) –

0

Làm việc này cho tôi: -

profileTracker = new ProfileTracker() { 
     @Override 
     protected void onCurrentProfileChanged(
       Profile oldProfile, 
       Profile currentProfile) { 

      if(currentProfile == null){ 
       tvUNameandEmail.setText(R.string.app_name); 
      } 
     } 
    }; 
4

Cảm ơn bạn Stan. Bạn đã giúp tôi giải quyết, nhưng đã cho tôi một thời gian. Để giúp những người khác, đó là toàn bộ mã:.

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(); 
Các vấn đề liên quan