请XMPP连接(使用咂嘴)在整个应用程序还活着 [英] Keep XMPP connection(using smack) alive throughout application
问题描述
我使用XMPP连接(使用咂嘴)聊天的机器人application.I已与openfire的连接,我也可以发送和接收message.But的问题是,当我走在XMPPClient.java活动然后做了connection.So我不能得到任何消息,直到没有往那个activity.So如何能取得在起跑线的连接,然后再使用其他的活动。code是在这2个环节的 ConnectionSettings文件和 chatscreen 在这是我们可以做的chat.In此链接注释行也是我的问题,所以也请看到的评论。
I am using the XMPP Connection(using smack) for chat in android application.I have made the connection with openfire and also i can send and receive the message.But the problem is that when i go in the XMPPClient.java activity then it made the connection.So i cant get any message till not go in that activity.So how can made the connection at the starting and then reuse at other activity.Code is in this 2 links ConnectionSettings file and the chatscreen in which we can do chat.In this link the comment line is also my questions so please also see that comment.
推荐答案
创建全局XMPPConnection对象,请使用以下功能可按并存储在全球XMPPConnection对象,并使用该连接对象无处不在。这是一个简单的,每增加一个例子。
Create global XMPPConnection object and Use below funciton and store in global XMPPConnection object and use that connection object everywhere. This is a sample gtalk example.
public XMPPConnection login() throws XMPPException {
ConnectionConfiguration config = new
ConnectionConfiguration("talk.google.com",5222,"gmail.com");
config.setSecurityMode(SecurityMode.required);
config.setTruststoreType("BKS");
config.setTruststorePath("/system/etc/security/cacerts.bks");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login(username, password);
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
try {
Thread.sleep(3000);
} catch (Exception ex) {
ex.printStackTrace();
}
return connection;
}
这篇关于请XMPP连接(使用咂嘴)在整个应用程序还活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!