实际上重新连接管理器没有任何问题。首先,您需要将连接侦听器添加到连接管理器。
connection.addConnectionListener(new ConnectionListener() {
@Override
public void reconnectionSuccessful() {
Log.i("","Successfully reconnected to the XMPP server.");
}
@Override
public void reconnectionFailed(Exception arg0) {
Log.i("","Failed to reconnect to the XMPP server.");
}
@Override
public void reconnectingIn(int seconds) {
Log.i("","Reconnecting in " + seconds + " seconds.");
}
@Override
public void connectionClosedOnError(Exception arg0) {
Log.i("","Connection to XMPP server was lost.");
}
@Override
public void connectionClosed() {
Log.i("","XMPP connection was closed.");
}
});
如果发生任何错误,连接关闭时将自动调用 connectionClosedOnError(Exception arg0)
public void connectionClosed() {
Log.i("","XMPP connection was closed.");
//You can manually call reconnection code if you want to reconnect on any connection close
}
然后检查它,这将调用 reconnectingin() 方法并尝试重新连接。
希望这会对你有所帮助。
使用以下代码检查连接
PingManager pingManager = PingManager.getInstanceFor(connection); pingManager.setPingInterval(5000);
添加用于 ping 失败处理的侦听器以处理连接是否连接,因为 isConnected 方法对于检查连接状态不可靠。
pingManager.registerPingFailedListener(PingFailedListener);
对于移动网络连接是非常大的问题,因此您需要使用广播接收器检查移动设备的网络连接,并且在数据重新连接时,您可以使用 pingMyServer 方法检查连接是否处于活动状态,如果您从服务器收到 ping 回复,则表示连接处于活动状态否则在 ping 失败时,您可以手动重新连接。