简体   繁体   English

Android Facebook SDK onUserInfoFetched方法不起作用

[英]Android Facebook SDK onUserInfoFetched method does not work

I have a problem about Facebook SDK for android. 我有关于Android版Facebook SDK的问题。 This code works for another activity of same application. 此代码适用于同一应用程序的另一活动。 I show to user the same FacebookLogin button in same xml file. 我向用户显示相同xml文件中的相同FacebookLogin按钮。 But in the other activity, when I login on facebook, onUserInfoFetched method does not work! 但是在其他活动中,当我登录Facebook时, onUserInfoFetched方法不起作用! So I can not get the user. 所以我无法获得用户。

The codes are in the same application. 这些代码在同一应用程序中。 But in different activities. 但是在不同的活动中。 I can login both of them. 我可以登录他们两个。 but I can not get the user. 但我无法获得用户。

The codes below: This is the code which successfully work: 以下代码:这是成功运行的代码:

public class FacebookPreference extends Preference {
    private LoginButton loginButton;
    private ProfilePictureView profilePictureView;
    public static GraphUser user;
    private Context mContext;

    private enum PendingAction {
        NONE, POST_PHOTO, POST_STATUS_UPDATE
    }

    public FacebookPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setWidgetLayoutResource(R.layout.preferences_item_list_facebook);
        this.mContext = context;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        profilePictureView = (ProfilePictureView) view.findViewById(R.id.profilePicture);
        loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                FacebookPreference.user = user;
                updateUI();

            }
        });

    }

    private void updateUI() {
        Session session = Session.getActiveSession();
        if (user != null) {
            profilePictureView.setProfileId(user.getId());
        } else {
            profilePictureView.setProfileId(null);
        }
    }
}

This is the code which does not work. 这是无效的代码。

public class FacebookLoginActivity extends Activity{

    LoginButton button;
    private ProfilePictureView profilePictureView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences_item_list_facebook);


        button = (LoginButton) findViewById(R.id.login_button);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);

        button.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                FacebookPreference.user = user;
                if(FacebookPreference.user != null)
                    Toast.makeText(FacebookLoginActivity.this,  FacebookPreference.user.getName().toString(), Toast.LENGTH_LONG).show();
                updateUI();

            }
        });

    };


    private void updateUI() {
        Session session = Session.getActiveSession();
        if (FacebookPreference.user != null) {
            profilePictureView.setProfileId(FacebookPreference.user.getId());
        } else {
            profilePictureView.setProfileId(null);
        }
    }
}

The xml File: xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/menu_item_back"
    android:orientation="horizontal" >

    <com.facebook.widget.ProfilePictureView
        android:id="@+id/profilePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        facebook:preset_size="normal" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"
        android:orientation="vertical" >

        <com.facebook.widget.LoginButton
            android:id="@+id/login_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            facebook:confirm_logout="false"
            facebook:fetch_user_info="true" >
        </com.facebook.widget.LoginButton>
    </LinearLayout>

</LinearLayout>

Thanks for answers. 感谢您的回答。

If you look at the other activity where this code works, you'll notice a UiLifecycleHelper member that shows up on each of the Activity lifecycle methods, as in: 如果您查看此代码可以工作的其他活动,您会注意到一个UiLifecycleHelper成员出现在每个Activity生命周期方法中,如下所示:

@Override
protected void onResume() {
    super.onResume();
    uiHelper.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    uiHelper.onPause();
}

etc. 等等

Implement this as well, and the callback should work. 还要实现它,回调应该起作用。

Just Look at this this working nice for me And Look At this Answer Facebook Sdk User details 看看这个对我的工作不错,看看这个答案Facebook Sdk用户详细信息

 private String UserInfoDisplay(GraphUser user) {    

    StringBuilder userInfo = new StringBuilder("");              
    String Name =  user.getName();
    String Id =   user.getId();
    String lastname =    user.getLastName();
    String firstname =  user.getFirstName();
    String getUsername =  user.getUsername();             

    String get_gender = (String) user.getProperty("gender");


    String image_url = "http://graph.facebook.com/"+user.getId()+"/picture?type=square";        

    Log.i("User information == >", Name +Id + lastname+ 
            firstname + image_url + get_gender +  getUsername + "usermail"+User_mail  );

    text.setText(Name + "  mail = " +User_mail);

    return userInfo.toString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM