Android Facebook SDK:Profile.getCurrentProfile()为什么第一次总是返回null?
我使用Facebook SDK中提供的LoginButton类在我的Android应用程序中实现Facebook登录。 按照说明操作后,一切正常。我在我的应用程序中唯一不同的地方是,我在一个片段上添加了登录按钮,以避免用大量代码污染登录活动。 但这里有一个问题: 我想获取用户的公共配置文件信息。我使用Android Facebook SDK:Profile.getCurrentProfile()为什么第一次总是返回null?,android,facebook,sdk,facebook-login,Android,Facebook,Sdk,Facebook Login,我使用Facebook SDK中提供的LoginButton类在我的Android应用程序中实现Facebook登录。 按照说明操作后,一切正常。我在我的应用程序中唯一不同的地方是,我在一个片段上添加了登录按钮,以避免用大量代码污染登录活动。 但这里有一个问题: 我想获取用户的公共配置文件信息。我使用Profile.getCurrentProfile()调用。以下是完整的代码: // Called in onCreate: private void attemptLogin() {
Profile.getCurrentProfile()调用。以下是完整的代码:
// Called in onCreate:
private void attemptLogin() {
mCallbackManager = CallbackManager.Factory.create();
FacebookCallback<LoginResult> mFacebookCallback = getFacebookCallback();
mLoginButton.registerCallback(mCallbackManager, mFacebookCallback);
}
//在onCreate中调用:
私有void attemptLogin(){
mCallbackManager=CallbackManager.Factory.create();
FacebookCallback mFacebookCallback=getFacebookCallback();
registerCallback(mCallbackManager,mFacebookCallback);
}
mFacebookCallback代码如下:
private FacebookCallback<LoginResult> getFacebookCallback() {
return new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if (currentProfile != null && oldProfile != null) {
mProfile = currentProfile;
// Get the new email and fire the home page of the activity, when email is available
requestUserEmail();
}
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
if (null != loginResult.getAccessToken()) {
String userId = loginResult.getAccessToken().getUserId();
Log.d(TAG, "Successfully Logged-In to Facebook");
Toast.makeText(getActivity(), "Logged-In to Facebook:" + userId, Toast.LENGTH_SHORT).show();
mProfile = Profile.getCurrentProfile();
// Now that login is successful, email can be requested
requestUserEmail();
}
}
@Override
public void onCancel() {
// App code
Log.d(TAG, "Cancelled Log-In to Facebook");
Toast.makeText(getActivity(), "Cancelled Log-In to Facebook", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d(TAG, "Error while Logging-In to Facebook");
Toast.makeText(getActivity(), "Error while Logging-In to Facebook", Toast.LENGTH_SHORT).show();
}
};
}
private FacebookCallback getFacebookCallback(){
返回新的FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
mProfileTracker=newprofiletracker(){
@凌驾
更改当前配置文件时受保护的无效(配置文件oldProfile、配置文件currentProfile){
if(currentProfile!=null&&oldProfile!=null){
mProfile=当前配置文件;
//获取新电子邮件并在电子邮件可用时启动活动主页
requestUserEmail();
}
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
if(null!=loginResult.getAccessToken()){
字符串userId=loginResult.getAccessToken().getUserId();
Log.d(标记“成功登录Facebook”);
Toast.makeText(getActivity(),“登录Facebook:+userId,Toast.LENGTH_SHORT.show()”;
mProfile=Profile.getCurrentProfile();
//现在登录成功,可以请求电子邮件了
requestUserEmail();
}
}
@凌驾
公开作废{
//应用程序代码
Log.d(标签“取消登录Facebook”);
Toast.makeText(getActivity(),“已取消登录Facebook”,Toast.LENGTH_SHORT.show();
}
@凌驾
public void onError(facebook异常){
//应用程序代码
Log.d(标记“登录Facebook时出错”);
Toast.makeText(getActivity(),“登录Facebook时出错”,Toast.LENGTH_SHORT.show();
}
};
}
我可以得到AccessToken甚至电子邮件。但是对于应用程序的第一次安装,Profile.getCurrentProfile()始终返回null
我还注意到,onCurrentProfileChanged()
从未真正被调用,因为我希望在第一次尝试登录时调用它。我不确定onCurrentProfileChanged()
相关代码是否应该在onSuccess()中。但根据这个问题,人们似乎通过这样做获得了更多的运气
我做错了什么?我需要做些什么来确保在第一步中获得配置文件信息?调试几分钟后,我发现了明显的问题。在下面的代码中:
if (currentProfile != null && oldProfile != null) {
mProfile = currentProfile;
// Get the new email and fire the home page of the activity, when email is available
requestUserEmail();
}
第一次,oldProfile始终为空。因此,mProfile永远不会设置为当前配置文件
第二,我不确定这是否有什么不同,但它正在改变
mLoginButton.setReadPermissions(Arrays.asList("user_friends", EMAIL));
到
也许会有帮助
另外,为了完成,只要在onCurrentProfileChanged
中请求电子邮件(在本例中调用requestUserEmail()
函数)一次就足够了,我将在git上发布它。顺便问一下,你是否在Facebook应用程序上启用了单点登录开关?这似乎有帮助。你可以在Facebook开发者门户网站上创建应用程序并进入它的设置(我做过的)之后完成。顺便说一句,我通过删除Profile.getCurrentProfile()使代码正常工作代码>从onSuccess()
方法,并将其余代码移动到onCurrentProfileChanged()
。太好了!:)我也是这么做的。我不再依赖getCurrentProfile()。而是在ProfileTracker上更新配置文件信息,如果以及当它更改时,这里也回答了这个问题:
mLoginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends", EMAIL));