我不知道如何在android http://xmpp.org/extensions/xep-0304.html中实现keepalive连接。我正在我的应用程序中使用asmack服务。连接中断两个小时后。我使用重新连接功能来保持活着但不起作用
public void reconnect(){
try{
connection = Koneksi.getInstance().getConnection();
}catch(NullPointerException e){
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
String username = pref.getString(PREF_USERNAME, null);
String password = pref.getString(PREF_PASSWORD, null);
System.out.println("FDATOS EN MYSERVICE " + username + " " + username);
try {
Koneksi.getInstance().init();
Koneksi.getInstance().performLogin(username, password);
Koneksi.getInstance().setStatus(true, "");
// Koneksi.getInstance().getConnection().getRoster().setSubscriptionMode(SubscriptionMode.manual);
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
答案 0 :(得分:1)
试试这种方式
将连接侦听器添加到xmmpp连接对象
connection.addConnectionListener(new ConnectionListener() {
@Override
public void reconnectionSuccessful() {
}
@Override
public void reconnectionFailed(Exception arg0) {
// TODO Auto-generated method stub
}
@Override
public void reconnectingIn(int arg0) {
}
@Override
public void connectionClosedOnError(Exception arg0) {
reconnect();
}
@Override
public void connectionClosed() {
reconnect();
}
});