This blog covers how to integrate aSmack (Smack with some patches to support in Android). Also SMACK 4.1 provides native support for Android which is currently in beta development stage.
Following jar files are requires for the XMPP integration in android:-
1) aSmack
2) dns java (Smack requires dnsjava for DNS SRV record lookup. You need to add the dnsjava library to your project's libraries.)
After adding the above library in your project you need to call SmackAndroid.init(Context)(inorg.jivesoftware.smack) in order to initialize Smack on Android or Smack won't work as expected!.
Now you successfully integrated the aSmack to your application.
For connecting XMPP Server:-
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(HOST, PORT);
connectionConfiguration.setSecurityMode(SecurityMode.disabled);
connectionConfiguration.setReconnectionAllowed(true);
XmppConnection connection = new XMPPTCPConnection(connectionConfiguration);
//Now add connection listener
/*
* Add Connection Listener to XMPP Connection
*/
connection.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XmppConnection connection) {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "CONNECTED");
}
@Override
public void authenticated(XmppConnection connection) {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "AUTHENTICATED");
}
@Override
public void connectionClosed() {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "CLOSED");
}
@Override
public void connectionClosedOnError(Exception e) {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "CONNECTED CLOSED ON ERROR");
}
@Override
public void reconnectingIn(int seconds) {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "RECONNECTINGIN");
}
@Override
public void reconnectionSuccessful() {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "CONNECTED SUCCESSFULLY");
}
@Override
public void reconnectionFailed(Exception e) {
// TODO Auto-generated method stub
Log.i("XMPP CONNECTION", "CONNECTED FAILED");
}
});
Now call connect method
try {
connection.connect();
connection.login("Username","Password");
}
catch (SmackException.ConnectionException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
// fetch incoming messages:-
private void fetchIncomingMessage() {
if (connection.isConnected()) {
PacketFilter workspaceMessageFilter=new PacketTypeFilter(Message.class);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {
final Message message = (Message) packet;
String type=String.valueOf(message.getType());
if(type.equals("chat")){
Log.i("MESSAGE BODY NORMAL MESSAGE", message.getBody());
}
}
},workspaceMessageFilter);
}
}
// send chat message
Message normalMessage = new Message(to, Message.Type.chat);
normalMessage.setBody(message, "current timestamp");
try{
connection.sendPackets(normalMessage);
} catch(Exception ex)
{
e.printStackTrace();
}
0 Comment(s)