編輯:關於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會出錯 Listitems = 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介紹了Android平台上的懸浮窗WindowManager,WindowManager懸浮窗可以懸浮在Android設備上的桌面窗口之上,但是WindowMa
前一節我們學習了Intent的基本用法,多個Activity之間的轉跳並傳遞信息。 今天我們使用Intnet來完成一些 特殊的操作,比如撥打電話,發送短信,浏覽網頁等……
前言因為這個RxJava內容不算少,而且應用場景非常廣,所以這個關於RxJava的文章我們會陸續更新,今天就來先來個入門RxJava吧初識RxJava什麼是Rx很多教程在
在上一篇博文《Android之——殺死用戶選中的進程(釋放進程占用的空間)》一文中,向大家介紹了如何殺死用戶選中的進程,但是,遺留了一個問題,那就