解雇facebook登录对话框android

时间:2016-04-27 10:04:55

标签: android facebook facebook-android-sdk

我已将 facebook sdk v-4 与我的Android应用集成。在这里,我在2个活动中登录了facebook登录。

1)在一项活动中,fb登录通过 LoginButton 完成。成功登录后,fb登录对话框会被facebook sdk自动解除。

代码:

fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        ProfileTracker profileTracker;

        @Override
        public void onSuccess(LoginResult loginResult) {
            Logger.d("FBLOGIN", "login successful. User ID: "
                    + loginResult.getAccessToken().getUserId()
                    + "\n" +
                    "Auth Token: "
                    + loginResult.getAccessToken().getToken());

            com.facebook.Profile fbProfile = com.facebook.Profile.getCurrentProfile();

            if (fbProfile == null) {
                profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(com.facebook.Profile oldProfile, com.facebook.Profile currentProfile) {
                        profileTracker.stopTracking();
                        doTaskWithFbProfile(currentProfile);
                    }
                };
                profileTracker.startTracking();
            } else {
                doTaskWithFbProfile(fbProfile);
            }

        }

        @Override
        public void onCancel() {
            Logger.d(TAG, "fb Login attempt canceled.");
        }

        @Override
        public void onError(FacebookException e) {
            Logger.e(TAG, "fb Login attempt failed." + e);
            Toast.makeText(LoginActivity.this,R.string.str_please_check_your_internet_connection,Toast.LENGTH_LONG).show();
        }
    });

 private void doTaskWithFbProfile(com.facebook.Profile fbProfile) {
    Logger.d("FBLOGIN", "id - " + fbProfile.getId());
    Logger.d("FBLOGIN", "name - " + fbProfile.getName());
    Logger.d("FBLOGIN", "pic - " + fbProfile.getProfilePictureUri(150, 150));

    com.android.circlecare.models.common.Profile profile = new com.android.circlecare.models.common.Profile();
    profile.setFBID(fbProfile.getId());
    profile.setName(fbProfile.getName());
    profile.setProfilePhoto(fbProfile.getProfilePictureUri(150, 150).toString());

    Intent intent = new Intent(this, LoginWithFacebookActivity.class);
    intent.putExtra(LoginWithFacebookActivity.EXTRA_PROFILE, profile);

    startActivity(intent);
    finish();

}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

2)另一方面,我使用 LoginManager 登录fb。成功登录后,登录对话框不会被取消,而是在登录后保持不变,对话框不会再出现。

代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

 FacebookSdk.sdkInitialize(this.getApplicationContext());

 mFacebookCallback = new FacebookCallback<LoginResult>() {
        ProfileTracker profileTracker;

        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile fbProfile = Profile.getCurrentProfile();

            if (fbProfile == null) {
                profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                        profileTracker.stopTracking();
                        getAllFriends();
                    }
                };
                profileTracker.startTracking();
            } else {
                getAllFriends();
            }
        }

        @Override
        public void onCancel() {
            Logger.d(TAG, "fb Login attempt canceled.");
        }

        @Override
        public void onError(FacebookException error) {
            Logger.d(TAG, "Error : " + error.getMessage());
            finish();
        }
    };

    callbackManager = CallbackManager.Factory.create();

    if (hasAccess)
        getAllFriends();
    else
        loginWorks();
}


  private void loginWorks() {
    loginManager = LoginManager.getInstance();
    loginManager.registerCallback(callbackManager, mFacebookCallback);

    ArrayList<String> publishPermission = new ArrayList<>();
    publishPermission.add("publish_pages");
    publishPermission.add("publish_actions");
    loginManager.logInWithPublishPermissions(this, publishPermission);

    ArrayList<String> readPermission = new ArrayList<>();
    readPermission.add("user_friends");
    readPermission.add("public_profile");
    readPermission.add("user_about_me");
    loginManager.logInWithReadPermissions(this, readPermission);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

我在两种情况下都使用了几乎相同的代码,但我想知道为什么登录对话框在使用LoginManager时不会被解雇。

任何人都可以指出问题或是否有任何api解雇fb登录对话框(网页视图)。

1 个答案:

答案 0 :(得分:1)

在你的loginWorks()方法中,你实际上是两次调用登录,这可能实际上显示了两个不同的登录屏幕,以及为什么看到对话框在完成其中一个之后没有消失。尝试删除loginWithPublishPermissions调用,如果确实需要,请将其放入读权限调用的回调中。