XMPP——Smack[4]状态,心情,头像更改
2014-11-20 16:39
316 查看
这里写完,最基本的IM功能也就完了,
还剩下个发送接收文件,离线消息扩展等等
呵呵,三天时间,看的不是很深入,欢迎大家补充呀
1. 修改自身状态
包括上线,隐身,对某人隐身,对某人上线
[java] view plaincopyprint?
public static void updateStateToAvailable(XMPPConnection connection)
{
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
}
public static void updateStateToUnAvailable(XMPPConnection connection)
{
Presence presence = new Presence(Presence.Type.unavailable);
connection.sendPacket(presence);
}
public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
{
Presence presence = new Presence(Presence.Type.unavailable);
presence.setTo(userName);
connection.sendPacket(presence);
}
public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
{
Presence presence = new Presence(Presence.Type.available);
presence.setTo(userName);
connection.sendPacket(presence);
}
2. 心情修改
[java] view plaincopyprint?
/**
* 修改心情
* @param connection
* @param status
*/
public static void changeStateMessage(XMPPConnection connection,String status)
{
Presence presence = new Presence(Presence.Type.available);
presence.setStatus(status);
connection.sendPacket(presence);
}
3. 修改用户头像
有点麻烦,主要是读入图片文件,编码,传输之
[java] view plaincopyprint?
public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException
{
VCard vcard = new VCard();
vcard.load(connection);
byte[] bytes;
bytes = getFileBytes(f);
String encodedImage = StringUtils.encodeBase64(bytes);
vcard.setAvatar(bytes, encodedImage);
vcard.setEncodedImage(encodedImage);
vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
+ encodedImage + "</BINVAL>", true);
ByteArrayInputStream bais = new ByteArrayInputStream(
vcard.getAvatar());
Image image = ImageIO.read(bais);
ImageIcon ic = new ImageIcon(image);
vcard.save(connection);
}
private static byte[] getFileBytes(File file) throws IOException {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
int bytes = (int) file.length();
byte[] buffer = new byte[bytes];
int readBytes = bis.read(buffer);
if (readBytes != buffer.length) {
throw new IOException("Entire file not read");
}
return buffer;
} finally {
if (bis != null) {
bis.close();
}
}
}
4. 补充,用户状态的监听
即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器
[java] view plaincopyprint?
final Roster roster = Client.getRoster();
roster.addRosterListener(
new RosterListener() {
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesAdded");
}
@Override
public void entriesDeleted(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesDeleted");
}
@Override
public void entriesUpdated(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesUpdated");
}
@Override
public void presenceChanged(Presence arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"presenceChanged");
}
});
转:http://blog.csdn.net/wklken/article/details/6460117
还剩下个发送接收文件,离线消息扩展等等
呵呵,三天时间,看的不是很深入,欢迎大家补充呀
1. 修改自身状态
包括上线,隐身,对某人隐身,对某人上线
[java] view plaincopyprint?
public static void updateStateToAvailable(XMPPConnection connection)
{
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
}
public static void updateStateToUnAvailable(XMPPConnection connection)
{
Presence presence = new Presence(Presence.Type.unavailable);
connection.sendPacket(presence);
}
public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
{
Presence presence = new Presence(Presence.Type.unavailable);
presence.setTo(userName);
connection.sendPacket(presence);
}
public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
{
Presence presence = new Presence(Presence.Type.available);
presence.setTo(userName);
connection.sendPacket(presence);
}
2. 心情修改
[java] view plaincopyprint?
/**
* 修改心情
* @param connection
* @param status
*/
public static void changeStateMessage(XMPPConnection connection,String status)
{
Presence presence = new Presence(Presence.Type.available);
presence.setStatus(status);
connection.sendPacket(presence);
}
3. 修改用户头像
有点麻烦,主要是读入图片文件,编码,传输之
[java] view plaincopyprint?
public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException
{
VCard vcard = new VCard();
vcard.load(connection);
byte[] bytes;
bytes = getFileBytes(f);
String encodedImage = StringUtils.encodeBase64(bytes);
vcard.setAvatar(bytes, encodedImage);
vcard.setEncodedImage(encodedImage);
vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
+ encodedImage + "</BINVAL>", true);
ByteArrayInputStream bais = new ByteArrayInputStream(
vcard.getAvatar());
Image image = ImageIO.read(bais);
ImageIcon ic = new ImageIcon(image);
vcard.save(connection);
}
private static byte[] getFileBytes(File file) throws IOException {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
int bytes = (int) file.length();
byte[] buffer = new byte[bytes];
int readBytes = bis.read(buffer);
if (readBytes != buffer.length) {
throw new IOException("Entire file not read");
}
return buffer;
} finally {
if (bis != null) {
bis.close();
}
}
}
4. 补充,用户状态的监听
即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器
[java] view plaincopyprint?
final Roster roster = Client.getRoster();
roster.addRosterListener(
new RosterListener() {
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesAdded");
}
@Override
public void entriesDeleted(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesDeleted");
}
@Override
public void entriesUpdated(Collection<String> arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"entriesUpdated");
}
@Override
public void presenceChanged(Presence arg0) {
// TODO Auto-generated method stub
System.out.println("--------EE:"+"presenceChanged");
}
});
转:http://blog.csdn.net/wklken/article/details/6460117
相关文章推荐
- XMPP——Smack[4]状态,心情,头像更改(转)
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack状态,心情,头像更改
- XMPP——Smack[4]状态,心情,头像更改 (转)
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack[4]状态,心情,头像更改
- XMPP——Smack[3]用户列表,头像,组操作,用户操作(转)
- Android基于XMPP Smack openfire 开发的聊天室(七) 【成员状态、自身状态】
- Openfire XMPP asmack开发 android客户端上传用户头像
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- XMPP——Smack[3]用户列表,头像,组操作,用户操作
- 基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- Android基于XMPP Smack openfire 开发的聊天室(七) 【成员状态、自身状态】
- XMPP——Smack[3]用户列表,头像,组操作,用户操作
- Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作
- XMPP——Smack[3]用户列表,头像,组操作,用户操作 (转)