Hi,
I am developing a Chat application… .
Here are the development system configurations …
Smack 4.2.0
Here is My Code snippet:
InetAddress addr = null;
try {
addr = InetAddress.getByName(mServiceName);
System.out.println("addr:"+addr);
} catch (UnknownHostException e) {
e.printStackTrace();
}
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return false;
}
};
XMPPTCPConnectionConfiguration connectionConfiguration= XMPPTCPConnectionConfiguration.builder()
.setXmppDomain(mServiceName).setHost(mServiceName).setResource("355557851080267078").setKeystoreType(null)
.setSendPresence(true)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).setPort(5222).setHostnameVerifier(verifier)
.setHostAddress(addr)
.setCompressionEnabled(true).build();
SmackConfiguration.DEBUG=true;
XMPPTCPConnection.setUseStreamManagementDefault(true);
mConnection= new XMPPTCPConnection(connectionConfiguration);
mConnection.setUseStreamManagement(true);
mConnection.setUseStreamManagementResumption(true);
mConnection.setPreferredResumptionTime(5);
mConnection.addConnectionListener(this);
ServerPingWithAlarmManager.getInstanceFor(mConnection).setEnabled(true);
pingManager=PingManager.getInstanceFor(mConnection);
pingManager.setPingInterval(5000);
try {
Log.d(LOGTAG,"Calling Connect");
mConnection.connect();
mConnection.login(new PrefManager(mApllicationContext).getphonenumber(),"9009");
Log.d(LOGTAG,"login() called");
}catch (InterruptedException e){
e.printStackTrace();
}
add connection listener-
@Override
public void connected(XMPPConnection connection) {
Log.d(LOGTAG,"connected.");
// mConnectionState=ConnectionState.CONNECTING;
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
// mConnectionState = ConnectionState.ONLINE;
Log.d(LOGTAG, "authenticated." + mConnection.isAuthenticated());
// SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(mApllicationContext);
// preferences.edit().putBoolean("XMPP loged In ",false).commit();
Intent i = new Intent(Constants.BroadCastMessages.UI_AUTHENTICATED);
i.setPackage(mApllicationContext.getPackageName());
mApllicationContext.sendBroadcast(i);
Log.d(LOGTAG, "send the broadcast that we are authenticate.");
}
@Override
public void connectionClosed() {
Log.d(LOGTAG,"XMPP connection was closed.");
// mConnectionState=ConnectionState.OFFILINE;
}
@Override
public void connectionClosedOnError(Exception e) {
Log.d(LOGTAG,"Connection to XMPP server was lost."+System.currentTimeMillis());
// mConnectionState=ConnectionState.OFFILINE;
}
@Override
public void reconnectionSuccessful() {
Log.d(LOGTAG,"Successfully reconnected to the XMPP server.");
}
@Override
public void reconnectionFailed(Exception arg0) {
Log.d(LOGTAG,"Failed to reconnect to the XMPP server.");
}
@Override
public void reconnectingIn(int seconds) {
reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
reconnectionManager.enableAutomaticReconnection();
reconnectionManager.setEnabledPerDefault(true);
pingManager = PingManager.getInstanceFor(mConnection);
pingManager.setPingInterval(5000);
try {
pingManager.pingMyServer();
pingManager.pingMyServer(true,5000);
pingManager.pingServerIfNecessary();
pingManager.registerPingFailedListener(new PingFailedListener() {
@Override
public void pingFailed() {
disconnect();
Toast.makeText(mApllicationContext,"pingFailed",Toast.LENGTH_SHORT).show();
try {
mConnection.connect();
mConnection.login(new PrefManager(mApllicationContext).getphonenumber(),"9009");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- I have a background service that is continuously running.
My assumption is that is happening when “Phone Network switches from Wifi-to-Data or Data-to-Wifi”. In the meantime, Re-connection Manager not working. Although, I am not sure about it.
How will be Re-connect a application?
Please Help !!!