Monday, March 01, 2010

XMPP / asmack / android / Google Talk

OK, I finally figured out how to send and receive XMPP instant messages from my gmail account using asmack with an Android 2.1 AVD. For some reason, the event-driven message receive code doesn't work, but this polling example code does. Here's my latest source:


// DON'T FORGET TO INCLUDE THE INTERNET PERMISSION IN YOUR MANIFEST.XML

package com.example.HelloFormStuff;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

public class HelloFormStuffActivity extends Activity {
public int state = 0;
private static final String TAG = "HelloFormStuffActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

new Thread(new Runnable() {
public void run() {
//XMPPConnection xmpp = new XMPPConnection("jabber.iitsp.com");
XMPPConnection xmpp = new XMPPConnection("gmail.com");
try {
xmpp.connect();

// for other jabber accounts, truncate after the @
//xmpp.login("username", "password");

// for gtalk / gmail, include the @
xmpp.login("your-gmail-account@gmail.com", "your-gmail-password");

} catch (XMPPException e) {
Log.v(TAG, "Failed to connect to " + xmpp.getHost());
e.printStackTrace();
}
ChatManager chatmanager = xmpp.getChatManager();
Chat newChat = chatmanager.createChat("friend@gmail.com", new MessageListener() {
// THIS CODE NEVER GETS CALLED FOR SOME REASON
public void processMessage(Chat chat, Message message) {
try {
Log.v(TAG, "Got:" + message.getBody());
chat.sendMessage(message.getBody());
} catch (XMPPException e) {
Log.v(TAG, "Couldn't respond:" + e);
}
Log.v(TAG, message.toString());
}
});

// Send something to friend@gmail.com
try {
newChat.sendMessage("OMNOMNOM");
} catch (XMPPException e) {
Log.v(TAG, "couldn't send:" + e.toString());
}

// Accept only messages from friend@gmail.com
PacketFilter filter
= new AndFilter(new PacketTypeFilter(Message.class),
new FromContainsFilter("friend@gmail.com"));

// Collect these messages
PacketCollector collector = xmpp.createPacketCollector(filter);

while(true) {
Packet packet = collector.nextResult();

if (packet instanceof Message) {
Message msg = (Message) packet;
// Process message
Log.v(TAG, "Got message:" + msg.getBody());
}
}

}

}).start();

//setContentView(this);
}
}

15 comments:

Unknown said...

Many thanks for this!
will try it out and let you know.

- Joseph.

Unknown said...

If you had a little GUI such that things could be more visual, that'll probably be a lot better.

Thanks all the same.
Joseph.

Unknown said...

I just came back to say that your was very good and helped me lots.

I have actually developed a GUI to impress my friends!

Thanks again.

Joseph.

Н.Цолмон said...
This comment has been removed by a blog administrator.
Н.Цолмон said...
This comment has been removed by a blog administrator.
karthik kulkarni said...

hi!! i'm trying to implement gtalk.....
i have run the same code above.. but i'm getting an error statinh that:

Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY

Package con.Chat.GtalkTest requires unavailabe shared library com.google.android.gtalkservice


if i try to include this library it says.. cant resolve package!!

karthik kulkarni said...

please help me... i cant even see the class gtalkservice in com.google.android package

Unknown said...

Hello, first of all thank you for this easy to implement tutorial.
I have a problem with the message listener, it doesn't run and i need to know how to get a message..
Thank you very much!

Unknown said...

Hi - I was just wondering if anyone has gotten to the bottom of the problem with eventlisteners?

Juhani said...

Thank you for sharing this. It as very helpful!

Rajesh Nair said...

Iam not able to login to my gmail account.It throws an illegal state exception "Not Connected to Server".Pls help me out:)

Sharique said...

You saved my life :)

I am really thankful to u...

good luck for ur future :p

Anonymous said...

Instead of using the packet stuff it is better to use the following code on an Android Device:

http://stackoverflow.com/questions/5853945/xmpp-asmack-connection-packetlistener-issue

Mobility Talk said...

I got an error "could not find class
org.jivesoftware.smack.xmppConnection".

Anonymous said...

thanx man can u show me complete