我正在使用 XMPP 连接(使用 smack)在 android 应用程序中聊天。我已经与 openfire 建立了连接,我也可以发送和接收消息。但问题是当我进入 XMPPClient.java Activity 时然后它建立了连接。所以在不进入该 Activity 之前我无法收到任何消息。那么如何在开始时建立连接然后在其他 Activity 中重用。代码在这 2 个链接中 ConnectionSettings file和 chatscreen我们可以在其中聊天。在此链接中,评论行也是我的问题,因此也请参阅该评论。
最佳答案
创建全局 XMPPConnection 对象并使用下面的函数并存储在全局 XMPPConnection 对象中并在任何地方使用该连接对象。这是一个示例 gtalk 示例。
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;
}
关于android - 在整个应用程序中保持 XMPP 连接(使用 smack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11342920/