我试图在xmpp asmack中阻止用户,以下是代码:
try {
PrivacyListManager privacyManager = PrivacyListManager
.getInstanceFor(MyXMPPConnection.getXMPPConnection());
if (privacyManager == null) {
return false;
}
String Black_List = "blockedList";
PrivacyList[] plists = privacyManager.getPrivacyLists();
if (plists.length == 0) {// No blacklisted or is not listed, direct getPrivacyList error
List<PrivacyItem> items = new ArrayList<PrivacyItem>();
Log.i("", "addToPrivacyList plists.length==0");
PrivacyItem newitem = new PrivacyItem("jid", false, 100);
newitem.setValue(name);
items.add(newitem);
privacyManager.updatePrivacyList(Black_List, items);
privacyManager.setActiveListName(Black_List);
return true;
}
PrivacyList plist = privacyManager.getPrivacyList(Black_List);
if (plist != null) {
String ser = "@" + connection.getServiceName();
List<PrivacyItem> items = plist.getItems();
for (PrivacyItem item : items) {
String from = item.getValue().substring(0,
item.getValue().indexOf(ser));
Log.i("",
"addToPrivacyList item.getValue=" + item.getValue());
if (from.equalsIgnoreCase(name)) {
items.remove(item);
break;
}
}
PrivacyItem newitem = new PrivacyItem("jid", false, 100);
newitem.setValue(name + "@"
+ connection.getServiceName());
items.add(newitem);
Log.i("", "addToPrivacyList item.getValue=" + newitem.toXML());
Log.i("", "deleteFromPrivacyList items size=" + items.size());
privacyManager.updatePrivacyList(Black_List, items);
privacyManager.setActiveListName(Black_List);
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
我将privacyManager
视为空
PrivacyListManager privacyManager = PrivacyListManager
.getInstanceFor(MyXMPPConnection.getXMPPConnection());
连接很好,我正在努力解决这个问题。 SO帖子都没有帮助我。我想知道为什么privacyManager变为null。请帮我解决问题。谢谢。
答案 0 :(得分:0)
我解决了这个问题,我不知道为什么privacylist类没有加载到jvm onload.so我明确地将类加载为 Class.forName(PrivacyListManager.class.getName());`< / p>
try {
ProviderManager.getInstance().addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
Class.forName(PrivacyListManager.class.getName());
}