打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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. 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. });

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
基于xmpp openfire smack开发之smack类库介绍和使用[2]
java实现简单XMPP发送消息和文件的简单例子
基于XMPP协议的Android IM研究
SmackAPI中文版 Smack帮助文档中文版
Android客户端基于XMPP的IM(openfire+asmack)的聊天工具之环境搭建及与服务器建立连接(一)
一个gtalk的robot的简单实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服