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

53 篇文章 0 订阅
49 篇文章 0 订阅

这里写完,最基本的IM功能也就完了,

还剩下个发送接收文件,离线消息扩展等等

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

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. }

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. }

3. 修改用户头像

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

  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. }

4. 补充,用户状态的监听

即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有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. });

下一篇主要是文件传输和接收

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值