I wanted to do the same thing. This is how I did it (using a code that I found in this forum).
FirstI created a class MyPacketFilter
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.filter.*;
public class MyPacketFilter implements PacketFilter {
public MyPacketFilter() {
}
public boolean accept(Packet packet) {
return true;
}
}
I also created a class MyPacketListener
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.filter.*;
public class MyPacketListener implements PacketListener {
XMPPConnection con = null;
public MyPacketListener(XMPPConnection con) {
this.con = con;
}
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.filter.*;
public class MyPacketListener implements PacketListener {
XMPPConnection con = null;
public MyPacketListener(XMPPConnection con) {
this.con = con;
}
/**
*/
public void processPacket(Packet packet){
if (packet instanceof Message)
{
System.err.println(“true”);
Message msg = (Message)packet;
String body = msg.getBody();
System.err.println("FROM: "+packet.getFrom());
System.err.println("BODY: "+msg.getBody());
}
}
}
And finally I created a class ReceiveMsg
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.filter.*;
public class ReceiveMsg {
public static void main(String args[]) {
try {
XMPPConnection connection = new XMPPConnection(“localhost”);
Message msg;
MyPacketListener myPacketListener = new MyPacketListener(connection);
MyPacketFilter myFilter = new MyPacketFilter();
connection.login(“Test”, “park”);
connection.addPacketListener(myPacketListener , myFilter);
msg = connection.createChat(“Test@localhost”).nextMessage(5000);
} catch (Exception e){System.err.println(e);}
}
}
I am only using connection.createChat(“Test@localhost”).nextMessage(5000); so that the program will not end before receiving the message (and because I thought I could do it using the method nextMessage(). I failed .
Hope that will help you.
Nick.