android如何显示我所有的Facebook好友并邀请玩

时间:2015-02-07 06:59:50

标签: android facebook facebook-graph-api android-listview facebook-invite

我是Facebook整合新手 在我的应用程序中,我通过Facebook成功登录,

但是,我想,

  1. 使用邀请按钮

  2. 在列表中显示我的所有Facebook好友
  3. 邀请邀请后发送给他们

  4. 我很发现,但我不懂Facebook文档, 所以任何人请给我任何相关的例子,

    非常感谢。

2 个答案:

答案 0 :(得分:1)

希望this会帮助你 如果您想显示所有朋友,那么您必须合并两个API

  1. 通过调用Graph API invitable_friends here可以获得的未使用您的应用的朋友是代码的文档。
    (invitable_friends只有在您的FBApp 分类时才能获得 as 游戏
  2. 已经在使用您的应用的朋友可以通过致电Graph API朋友获取
  3. 在这里,如果您想要检索invitable_friends,那么您必须login with permission

    此处发送邀请的最后一件事是documentation

答案 1 :(得分:1)

我已经把一些重要的部分用于显示facebook的朋友们正在跟随。有关更多信息和任何问题,您必须参考,

https://developers.facebook.com/

https://developers.facebook.com/docs/games/unity/unity-tutorial

https://github.com/fbsamples/web-friend-smash-v1

https://github.com/fbsamples/android-friend-smash-v2

以下主要部分显示fb好友列表(尚未安装游戏) 在列表中显示Facebook好友:

按下按钮上的以下功能点击:

      Session session = Session.getActiveSession();
      if (session == null || !session.isOpened()) {
            return;
      }
      List<String> permissions = session.getPermissions();

      if (!permissions.contains("user_friends")) {

            askForFriendsForPlayPermission(session);

        } else {
            loadFriendsFromFacebook(new FriendsLoadedCallback() {

                @Override
                public void afterFriendsLoaded() {
                    // startGame();
                    close_button_value = 11;
                    img_close_button.setVisibility(View.VISIBLE);
                }

            });

        }

askForFriendsForPlayPermission(...):此功能有助于获取朋友权限,如果按是则显示fb好友列表列表,如下:

    private void askForFriendsForPlayPermission(final Session session) {
    // user has already said no once this session.
    if (application.hasDeniedFriendPermission()) {
    } else {
        new AlertDialog.Builder(HomeActivity.this)
                .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // User hit OK. Request Facebook friends
                        // permission.
                          requestFriendsPermission(AUTH_FRIENDS_PLAY_ACTIVITY_CODE);
                    }
                }).setNegativeButton(R.string.dialog_no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User hit cancel. Keep track of deny so
                        // that we
                        // only ask once per session
                        // and then just play the game.
                        application.setHasDeniedFriendPermission(true);

                    }
                }).setTitle(R.string.with_friends_dialog_title).setMessage(R.string.with_friends_dialog_message)
                .show();
    }
}

获取user_friends权限 - 调用:

    private void requestFriendsPermission(int requestCode) {
    // --//--Log.d("Permiision", "Requesting friends permissions.");
    Session.NewPermissionsRequest newFriendsPermissionsRequest = new Session.NewPermissionsRequest(this,
            "user_friends").setRequestCode(requestCode);
    Session.getActiveSession().requestNewReadPermissions(newFriendsPermissionsRequest);

}

加载facebook好友:

   private void loadFriendsFromFacebook(final FriendsLoadedCallback callback) {
   final Session session = Session.getActiveSession();
   RequestBatch requestBatch = new RequestBatch();
   Request invitableFriendsRequest = Request.newGraphPathRequest(session, "/me/invitable_friends",
            new Request.Callback() {

                @Override
                public void onCompleted(Response response) {

                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Log.e(CricoApplication.TAG, error.toString());
                        // handleError(error, true);
                    } else if (session == Session.getActiveSession()) {
                        if (response != null) {
                            // Get the result
                            GraphObject graphObject = response.getGraphObject();
                            JSONArray dataArray = (JSONArray) graphObject.getProperty("data");

                            List<JSONObject> invitableFriends = new ArrayList<JSONObject>();
                            if (dataArray.length() > 0) {
                                // Ensure the user has at least one friend
                                // ...
                                // fb_friends = new ArrayList<String>();
                                list_fb_friends = new ArrayList<HashMap<String, String>>();
                                for (int i = 0; i < dataArray.length(); i++) {
                                    invitableFriends.add(dataArray.optJSONObject(i));
                                    try {
                                        JSONObject json = dataArray.getJSONObject(i);
                                        String str_id = json.getString(TAG_ID);
                                        String str_first_name = json.getString(TAG_FIRST_NAME);

                                        JSONObject picture_obj = json.getJSONObject(TAG_PICTURE);
                                        JSONObject data_obj = picture_obj.getJSONObject(TAG_DATA);
                                        // String str_is_silhouette =
                                        // data_obj.getString(TAG_IS_SILHOUETTE);
                                        String str_url = data_obj.getString(TAG_URL);

   // put fb id and friends name in map and add to list view 
                                        map_fb_friends = new HashMap<String, String>();
                                        map_fb_friends.put("str_id", str_id);
                                        map_fb_friends.put("str_first_name", str_first_name);
                                        map_fb_friends.put("str_url", str_url);
                                        list_fb_friends.add(map_fb_friends);
                                        fb_friends.add(str_id);

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }


LazyAdapter_fb_friends adapter = new LazyAdapter_fb_friends(HomeActivity.this,
                                        list_fb_friends);
Your_list_view.setAdapter(adapter);
                            }

                            application.setInvitableFriends(invitableFriends);
                        }
                    }
                }

            });
    Bundle invitableParams = new Bundle();
    invitableParams.putString("fields", "id,first_name,picture");
    invitableFriendsRequest.setParameters(invitableParams);
    requestBatch.add(invitableFriendsRequest);

    // Get the user's list of friends.
    // This only returns friends who have installed the game.
    Request friendsRequest = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {

        @Override
        public void onCompleted(List<GraphUser> users, Response response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                Log.e(CricoApplication.TAG, error.toString());
                // handleError(error, true);
            } else if (session == Session.getActiveSession()) {
                // Set the friends attribute
                application.setFriends(users);
                callback.afterFriendsLoaded();
            }
        }
    });

您可以使用facebook-id邀请特定朋友。