使用xmpp + smack + openfire在android中阻止用户

时间:2015-06-08 11:21:28

标签: android xmpp openfire

在我的聊天应用程序中。我正在使用Smack库和Openfire服务器。我想阻止特定用户。

我正在尝试实现一个阻止特定用户但不能为我工作的函数。它不会给出任何错误或异常。

我的代码是

  XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx@xxx.com");
Activity A -> Activity B -> Activity C -> Activity A

2 个答案:

答案 0 :(得分:1)

    public List<String> getBlockedUserList(String userId) { 

    List<String> privacyList = new ArrayList<String>();
    try {
        PrivacyListManager privacyManager = PrivacyListManager
                .getInstanceFor(XMPPUtils.INSTANCE.connection);
        if (privacyManager == null) {
            return privacyList;
        }
        String ser = "@" + XMPPUtils.INSTANCE.XMPPService;
        PrivacyList plist = null;
        try {
            plist = privacyManager.getPrivacyList("public");
        } catch (NoResponseException e) {
            e.printStackTrace();
        } catch (NotConnectedException e) {
            e.printStackTrace();
        }
        if (plist != null) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {


                String from = item.getValue().substring(0,
                        item.getValue().indexOf(ser));

                if (userId.equals(from)) {

                    item.isAllow();
                }
                // privacyList.add(from);


            }
        } else {
            return privacyList;
        }
    } catch (XMPPException ex) {
    }
    return privacyList;
}

答案 1 :(得分:0)

使用Smack 4.1.0和Openfire 3.10.0,你可以实现像下面的阻止用户

public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) {

        String listName = "newList";
        List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid,
            userName,false, 1l);
    privacyItems.add(item);
    // Create the new list.

    try {
        PrivacyListManager privacyManager; 
        privacyManager = PrivacyListManager
                .getInstanceFor(connection);
        privacyManager.createPrivacyList(listName, privacyItems);

    } catch (XMPPException e) {
        System.out.println("PRIVACY_ERROR: " + e);
    }
    }

现在,如果您调用上述功能

XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx");    

在smack调试器中,你可以在iq节下面观察

<iq id="5W6tl-27" type="set">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

<iq to="xyz@test-xmpp-abc/Smack" id="5W6tl-27" type="result">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

希望这能解决您的问题