XMPP——Smack[4]狀態,心情,頭像更改


這裏寫完,最基本的IM功能也就完了,

還剩下個發送接收文件,離線消息擴展等等

 

呵呵,三天時間,看的不是很深入,歡迎大家補充呀

 

1.       修改自身狀態

包括上線,隱身,對某人隱身,對某人上線

  1. public static void updateStateToAvailable(XMPPConnection connection)  
  2.     {  
  3.         Presence presence = new Presence(Presence.Type.available);  
  4.         connection.sendPacket(presence);  
  5.      }  
  6.       
  7.     public static void updateStateToUnAvailable(XMPPConnection connection)  
  8.     {  
  9.         Presence presence = new Presence(Presence.Type.unavailable);  
  10.         connection.sendPacket(presence);  
  11.         }  
  12.       
  13.     public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)  
  14.     {  
  15.         Presence presence = new Presence(Presence.Type.unavailable);  
  16.         presence.setTo(userName);  
  17.         connection.sendPacket(presence);  
  18.     }  
  19.     public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)  
  20.     {  
  21.         Presence presence = new Presence(Presence.Type.available);  
  22.         presence.setTo(userName);  
  23.         connection.sendPacket(presence);  
  24.   
  25.     }  

2.       心情修改

  

  1. /** 
  2.      * 修改心情 
  3.      * @param connection 
  4.      * @param status 
  5.      */  
  6.     public static void changeStateMessage(XMPPConnection connection,String status)  
  7.     {  
  8.         Presence presence = new Presence(Presence.Type.available);  
  9.         presence.setStatus(status);  
  10.         connection.sendPacket(presence);  
  11.       
  12.     }  

3.       修改用戶頭像

有點麻煩,主要是讀入圖片文件,編碼,傳輸之

  1. public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException  
  2.     {  
  3.       
  4.         VCard vcard = new VCard();  
  5.         vcard.load(connection);  
  6.           
  7.             byte[] bytes;  
  8.             
  9.                 bytes = getFileBytes(f);  
  10.                 String encodedImage = StringUtils.encodeBase64(bytes);  
  11.                 vcard.setAvatar(bytes, encodedImage);  
  12.                 vcard.setEncodedImage(encodedImage);  
  13.                 vcard.setField("PHOTO""<TYPE>image/jpg</TYPE><BINVAL>"  
  14.                         + encodedImage + "</BINVAL>"true);  
  15.                   
  16.                   
  17.                 ByteArrayInputStream bais = new ByteArrayInputStream(  
  18.                         vcard.getAvatar());  
  19.                 Image image = ImageIO.read(bais);  
  20.                 ImageIcon ic = new ImageIcon(image);  
  21.                    
  22.              
  23.             
  24.             vcard.save(connection);  
  25.              
  26.     }  
  27.       
  28.     private static byte[] getFileBytes(File file) throws IOException {  
  29.         BufferedInputStream bis = null;  
  30.         try {  
  31.             bis = new BufferedInputStream(new FileInputStream(file));  
  32.             int bytes = (int) file.length();  
  33.             byte[] buffer = new byte[bytes];  
  34.             int readBytes = bis.read(buffer);  
  35.             if (readBytes != buffer.length) {  
  36.                 throw new IOException("Entire file not read");  
  37.             }  
  38.             return buffer;  
  39.         } finally {  
  40.             if (bis != null) {  
  41.                 bis.close();  
  42.             }  
  43.         }  
  44. }  

4.  補充,用戶狀態的監聽

 

即對方改變頭像,狀態,心情時,更新自己用戶列表,其實這裏已經有smack實現的監聽器

 

 

 

 

  1. final Roster roster = Client.getRoster();  
  2.           
  3.         roster.addRosterListener(  
  4.                 new RosterListener() {  
  5.   
  6.                     @Override  
  7.                     public void entriesAdded(Collection<String> arg0) {  
  8.                         // TODO Auto-generated method stub  
  9.                         System.out.println("--------EE:"+"entriesAdded");  
  10.                     }  
  11.   
  12.                     @Override  
  13.                     public void entriesDeleted(Collection<String> arg0) {  
  14.                         // TODO Auto-generated method stub  
  15.                         System.out.println("--------EE:"+"entriesDeleted");  
  16.                     }  
  17.   
  18.                     @Override  
  19.                     public void entriesUpdated(Collection<String> arg0) {  
  20.                         // TODO Auto-generated method stub  
  21.                         System.out.println("--------EE:"+"entriesUpdated");  
  22.                     }  
  23.   
  24.                     @Override  
  25.                     public void presenceChanged(Presence arg0) {  
  26.                         // TODO Auto-generated method stub  
  27.                         System.out.println("--------EE:"+"presenceChanged");  
  28.                     }     
  29.                       
  30.                 });  
  31.               

 


轉:http://blog.csdn.net/wklken/article/details/6460117



發佈了76 篇原創文章 · 獲贊 3 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章