Facebook的登录API 4.0:登录时Profile.getCurrentProfile空API、Facebook、getCurrentProfile、Profile

2023-09-12 09:35:58 作者:心痛的难受

由于某种原因, Profile.getCurrentProfile()显示了在时间空登录到Facebook的后面。这是导致我的应用程序,我的问题,因为我不能没有资料没有被空显示我的下一个活动。

For some reason Profile.getCurrentProfile() shows up null at times right after logging into facebook. This is causing problems for me in my app because I can't display my next activity without Profile not being null.

我之所以说这是空有时是因为如果我关闭我的应用程序,然后重新打开它出来,我能得到我的下一个活动的屏幕,但如果我没有登录,然后登录,档案是空似乎在短期内。

The reason I say it's null sometimes is because if I close out of my app and re-open it, I'm able to get to my next activity screen, but if I'm not already logged in, and then login, Profile is null it seems for a short period.

有没有解决或修复此?

推荐答案

就像@Hardi帕特尔说,你必须添加以下code(在的onSuccess()您实施的 FacebookCallback ):

Like @Hardi patel said, you have to add the following code (inside onSuccess() of your implementation of FacebookCallback):

LoginButton loginButton = (LoginButton) findViewById(R.id.btn_facebook);
loginButton.setReadPermissions("public_profile");
mCallbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {

    private ProfileTracker mProfileTracker;

    @Override
    public void onSuccess(LoginResult loginResult) {
        if(Profile.getCurrentProfile() == null) {
            mProfileTracker = new ProfileTracker() {
                @Override
                protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                    Log.v("facebook - profile", profile2.getFirstName());
                    mProfileTracker.stopTracking();
                }
            };
            mProfileTracker.startTracking();
        }
        else {
            Profile profile = Profile.getCurrentProfile();
            Log.v("facebook - profile", profile.getFirstName());
        }
    }

    @Override
    public void onCancel() {
        Log.v("facebook - onCancel", "cancelled");
    }

    @Override
    public void onError(FacebookException e) {
        Log.v("facebook - onError", e.getMessage());
    }
});

而在 onActivityResult()您必须添加以下行。如果不这样做, FacebookCallback 回调将不会被调用:

And in the onActivityResult() you must add the following lines. If you don't, FacebookCallback callbacks won't be called:

if (mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
    return;
}

编辑:

code更新@Alex Zezekalo 的建议,只调用 mProfileTracker.startTracking(); 如果 Profile.getCurrentProfile()返回空。

Code updated with @Alex Zezekalo's suggestion to only call mProfileTracker.startTracking(); if Profile.getCurrentProfile() returns null.