出于某种原因Profile.getCurrentProfile(),在使用FB API v4.0登录FaceBook后,有时会显示null.
这是造成问题,我在我的应用程序,因为我不能显示我的未来Activity与Profile被空.
我有时说它为空的原因是因为如果我关闭我的应用程序并重新打开它,我就可以进入下一个Activity,但如果我还没有登录,然后登录,Profile则为null.这似乎是一段短暂的时期.
有没有解决或解决这个问题?
Suf*_*ian 83
就像哈代说的那样,你必须创建一个实例ProfileTracker,它将开始跟踪配置文件更新(ProfileTracker.onCurrentProfileChanged()即将在用户的配置文件完成提取时调用).
以下是您需要登录FB并获取用户个人资料的完整代码:
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 oldProfile, Profile currentProfile) {
Log.v("facebook - profile", currentProfile.getFirstName());
mProfileTracker.stopTracking();
}
};
// no need to call startTracking() on mProfileTracker
// because it is called by its constructor, internally.
}
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());
}
});
Run Code Online (Sandbox Code Playgroud)
您必须覆盖您的活动或片段onActivityResult(),如下所示:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if you don't add following block,
// your registered `FacebookCallback` won't be called
if (mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
Run Code Online (Sandbox Code Playgroud)
使用Alex Zezekalo的建议更新代码,仅在调用mProfileTracker.startTracking();if时Profile.getCurrentProfile()返回null.
And*_*est 18
根据Sufian的回答,您还可以将新配置文件保存到Profile类:
@Override
public void onSuccess(LoginResult loginResult) {
ProfileTracker profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
this.stopTracking();
Profile.setCurrentProfile(currentProfile);
}
};
profileTracker.startTracking();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21305 次 |
| 最近记录: |