XMPP——Smack[4]状态,心情,头像更改

时间:2023-03-09 00:34:14
XMPP——Smack[4]状态,心情,头像更改

呵呵,三天时间,看的不是很深入,欢迎大家补充呀

  1. 修改自身状态

包括上线,隐身,对某人隐身,对某人上线

  1. public static void updateStateToAvailable(XMPPConnection connection)
  2. {
  3. Presence presence = new Presence(Presence.Type.available);
  4. connection.sendPacket(presence);
  5. }
  6. public static void updateStateToUnAvailable(XMPPConnection connection)
  7. {
  8. Presence presence = new Presence(Presence.Type.unavailable);
  9. connection.sendPacket(presence);
  10. }
  11. public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
  12. {
  13. Presence presence = new Presence(Presence.Type.unavailable);
  14. presence.setTo(userName);
  15. connection.sendPacket(presence);
  16. }
  17. public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
  18. {
  19. Presence presence = new Presence(Presence.Type.available);
  20. presence.setTo(userName);
  21. connection.sendPacket(presence);
  22. }
  1. 心情修改
  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. }
  1. 修改用户头像

有点麻烦,主要是读入图片文件,编码,传输之

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

即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器

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