原文:http://plplum.blog.163.com/blog/static/31032400201503015345948/
注意事项:
1、登录前要将状态设置为离线:
ConnectionConfiguration connectionConfig = new ConnectionConfiguration(192.168.1.82, 5222, "");
connectionConfig.setSendPresence(false);
ASmackMainActivity.connection = new XMPPConnection(connectionConfig);
2、接收完离线数据后,要将离线数据清除:
offlineManager.deleteMessages();
3、最后要将用户状态设置为在线。
ConnectionConfiguration connectionConfig = new ConnectionConfiguration(192.168.1.82, 5222, "");
connectionConfig.setSendPresence(false);
ASmackMainActivity.connection = new XMPPConnection(connectionConfig);
connection.login(用户名,密码);
OfflineMessageManager offlineManager = new OfflineMessageManager(ASmackMainActivity.connection);
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager.getMessages();
while(it.hasNext()){
org.jivesoftware.smack.packet.Message message = it.next();
Log.e(TAG, "收到离线消息, Received from 【" + message.getFrom() + "】 message: " + message.getBody());
}
//删除离线消息
offlineManager.deleteMessages();
//将状态设置成在线
Presence presence = new Presence(Presence.Type.available);
ASmackMainActivity.connection.sendPacket(presence);