I found this question: XMPP events on Android, and the answer explains a solution to the problem I am having. But I cannot seem to figure out how to get the packetListener to work. I have tried packetCollectors and packetListeners but noting seems to work.
I am unsure of where I should place the packet listeners. Should it go into the onCreate() method of the service, or in the onStartCommand(), or should it be put into a separate method that runs every few seconds?
My confusion is mostly with the concept of listeners. Is the listener always running, and will trigger as soon as a packet is received? And how do I make sure that the listener can ‘see’ the packet events coming in?
This is my current attempt at getting this to work:
public class XMPPService extends Service{
private static String TAG = "XMPPService";
XMPPConnection connection;
MultiUserChat muc;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId){
ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
connection = new XMPPConnection(config);
try
{
connection.connect();
Log.i(TAG,"connected");
}
catch (XMPPException e)
{
Log.e(TAG, "Connection Issue: ", e);
}
try
{
connection.login("user", "password");
muc = new MultiUserChat(connection, "room@conference.jabber.org");
muc.join("nick","password");
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
setConnection(connection);
}
catch (XMPPException e)
{
Log.e(TAG, "Connection Issue: ", e);
}
makeNotification("started");
return 1;
}
private void makeNotification(String msg){
Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MUCTestActivity.class), 2);
notification.setLatestEventInfo(this, "Title", msg, contentIntent);
NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nmgr.notify(123443, notification);
}
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i(TAG, "Got text [" + message.getBody() + "] from [" + fromName + "]");
}
else
{
Log.i(TAG,"null");
}
}
}, filter);
}
}
}
The PacketListener part looks right. It will be attached to smacks connection (the reader thread) and invoked by the connection if a packet, which matches the filter, arrives. You got the concept right.
Returning START_STICKY (please use here the constant and not the value) should also help to keep the service and therefore the connection alive.
Do you see the JID of the connection online? I am not sure if one can always send messages to a JID without being in the roster of the JID. Try enabling debugging in smack: Connection.DEBUG_ENABLED=true. If you have asmack, all debug messages will go into DBMS log. Also the emulator and eclipse with breakpoints are powerful tools to help you analyze the problem.