0

您好,我尝试从登录的用户那里获取用户名和用户 ID。

我尝试在我的 onActivityResuly 中使用此代码,但它没有被触发。

感谢您的帮助:)

final Session session = Session.getActiveSession();
                Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        // TODO Auto-generated method stub
                        if (session == Session.getActiveSession()) {
                            if (user != null) {
                                facebook_id = user.getId();//user id
                                facebook_name= user.getName();

                                Log.i("facebook_id", facebook_id);
                                Log.i("facebook_name", facebook_name);

                                new GetProfilePicture().execute();

                            }   
                        }   
                    }   
                }); 
                Request.executeBatchAsync(request);
                    super.onActivityResult(requestCode, resultCode, data2);
4

1 回答 1

2

我强烈建议在这里查看指南https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

Facebook 开发Best Explanation is available人员

对于您的情况,请按照第 6 步操作,这将完全满足您的需求!

试试这对我有用:

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

      // callback when session changes state
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {
               facebook_id = user.getId();//user id
                            facebook_name= user.getName();

                            Log.i("facebook_id", facebook_id);
                            Log.i("facebook_name", facebook_name);
              }
            }
          });
        }
      }
    });
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }
于 2013-08-23T13:43:26.380 回答