Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Xmpp 黑名單

Android Xmpp 黑名單

編輯:關於Android編程

簡介

該功能是基於Xmpp協議的ejabberd服務器黑名單的實現。黑名單的作用是把消息扼殺在服務器中。

由於功能強大,代碼實現簡單,這就不做任何介紹直接貼出代碼了,懂Xmpp同學一看便知。


功能代碼

功能分為:添加黑名單、刪除黑名單、獲取黑名單s,這些功能代碼經測試通過(只是實現功能,而未優化代碼),代碼如下:

	public boolean addToPrivacyList(String name) { // 添加到黑名單
		try {
			PrivacyListManager privacyManager = PrivacyListManager
					.getInstanceFor(XmppTool.getConnection());
			if (privacyManager == null) {
				return false;
			}

			PrivacyList[] plists = privacyManager.getPrivacyLists();
			if (plists.length == 0) {// 沒有黑名單或是名單中沒有列,直接getPrivacyList會出錯
				List items = new ArrayList();
				Log.i("", "addToPrivacyList plists.length==0");
				PrivacyItem newitem = new PrivacyItem("jid", false, 100);
				newitem.setValue("BLACKNAME" + "@"
						+ XmppTool.getConnection().getServiceName());
				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 = "@" + XmppTool.getConnection().getServiceName();

				List 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 + "@"
						+ XmppTool.getConnection().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 (XMPPException ex) {
			// Logger.getLogger(XMPP.class.getName()).log(Level.SEVERE, null,
			// ex);
		}
		return false;
	}

	public boolean deleteFromPrivacyList(String name) {// 刪除黑名單

		try {
			PrivacyListManager privacyManager = PrivacyListManager
					.getInstanceFor(XmppTool.getConnection());
			if (privacyManager == null) {
				return false;
			}
			PrivacyList plist = privacyManager.getPrivacyList(Black_List);
			if (plist != null) {
				String ser = "@" + XmppTool.getConnection().getServiceName();

				List items = plist.getItems();
				for (PrivacyItem item : items) {
					String from = item.getValue().substring(0,
							item.getValue().indexOf(ser));
					Log.i("",
							"deleteFromPrivacyList item.getValue="
									+ item.getValue());
					if (from.equalsIgnoreCase(name)) {
						Log.i("", "deleteFromPrivacyList find object");
						items.remove(item);
						break;
					}
				}
				Log.i("", "deleteFromPrivacyList items size=" + items.size());
				privacyManager.updatePrivacyList(Black_List, items);
			}
		} catch (XMPPException ex) {
		}
		return true;
	}

	public List getPrivacyList() { // 獲取所有黑名單
		List privacyList = new ArrayList();
		try {
			PrivacyListManager privacyManager = PrivacyListManager
					.getInstanceFor(XmppTool.getConnection());
			if (privacyManager == null) {
				return privacyList;
			}
			String ser = "@" + XmppTool.getConnection().getServiceName();
			PrivacyList plist = privacyManager.getPrivacyList(Black_List);
			if (plist == null) {// 沒有黑名單或是名單中沒有列,直接getPrivacyList會出錯
				List items = plist.getItems();
				for (PrivacyItem item : items) {
					String from = item.getValue().substring(0,
							item.getValue().indexOf(ser));
					privacyList.add(from);
				}
			} else {
				return privacyList;
			}
		} catch (XMPPException ex) {
		}
		return privacyList;
	}


結論
功能強大,代碼簡單。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved