ConnectToServer(){
final ProgressDialog dialog = ProgressDialog.show(ChatWindowFragmentActivity.this,
"Connecting...", "Please wait...", false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// Object of XmppClient class
XmppClient mXmppClient = new XmppClient();
/*
* // Create a connection ConnectionConfiguration connConfig =
* new ConnectionConfiguration(HOST, PORT);
*/
XMPPConnection connection = null;
try {
SmackAndroid.init(ChatWindowFragmentActivity.this);
connection = mXmppClient.connectionToXmppServer();
} catch (XMPPException e) {
// TODO Auto-generated catch block
// setConnection(null, null);
}
try {
mXmppClient.loginUser(connection, USERNAME, PASSWORD);
Log.i("XMPPChatDemoActivity",
"Logged in as" + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);
Roster roster = connection.getRoster();
/*
Fetch USER availability
*/
switch (isUserAvailable(connection)){
case 0:
imgAvailability.setBackgroundColor(Color.GRAY);
break;
case 1:
imgAvailability.setBackgroundColor(Color.GREEN);
break;
case 2:
imgAvailability.setBackgroundColor(Color.YELLOW);
break;
case 3:
imgAvailability.setBackgroundColor(Color.RED);
break;
default:
break;
}
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Log.d("XMPPChatDemoActivity",
"--------------------------------------");
Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
Log.d("XMPPChatDemoActivity",
"User: " + entry.getUser());
Log.d("XMPPChatDemoActivity",
"Name: " + entry.getName());
Log.d("XMPPChatDemoActivity",
"Status: " + entry.getStatus());
Log.d("XMPPChatDemoActivity",
"Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry
.getUser());
Log.d("XMPPChatDemoActivity", "Presence Status: "
+ entryPresence.getStatus());
Log.d("XMPPChatDemoActivity", "Presence Type: "
+ entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available)
Log.d("XMPPChatDemoActivity", "Presence AVAILABLE");
Log.d("XMPPChatDemoActivity", "Presence : "
+ entryPresence);
}
} catch (XMPPException e) {
e.printStackTrace();
Log.e("XMPPChatDemoActivity", "Failed to log in as "
+ USERNAME);
Log.e("XMPPChatDemoActivity", e.toString());
new ShowAlert(ChatWindowFragmentActivity.this,e.getMessage(), false).show(
getSupportFragmentManager(), TAG);
// setConnection(null, null);
}
dialog.dismiss();
}
});
t.start();
dialog.show();
}
And your method called inside it.