Show me the code! – By Davanum Srinivas

November 14, 2007

Sending XMPP messages in Android – Standalone sample (Ant build.xml/intellij project)

Filed under: Uncategorized — Davanum Srinivas @ 4:53 pm

Set up your XMPP Settings under Dev Tools


Download the and install the app

adb install XMPPSender.apk

The Zip has an Ant build.xml that you can use to build the complete project.

Run the app, enter your friend’s gtalk email and the text and press send


Here’s the code that gets a XMPP session

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the XmppService has been
            // established, giving us the service object we can use to
            // interact with the service.  We are communicating with our
            // service through an IDL interface, so get a client-side
            // representation of that from the raw service object.
            IXmppService xmppService = IXmppService.Stub.asInterface(service);

            try {
                mXmppSession = xmppService.getDefaultSession();
                if (mXmppSession == null) {
                    // this should not happen.
            } catch (DeadObjectException ex) {
                Log.e(LOG_TAG, "caught " + ex);


        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mXmppSession = null;

Here’s the code that actually sends the message

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (v == mUsernameField) {
            } else {
                // use XmppService to send data message to someone
                String username = mUsernameField.getText().toString();
                if (!isValidUsername(username)) {

                if (mXmppSession == null) {

                try {
                    mXmppSession.sendTextMessage(username, 0,
                } catch (DeadObjectException ex) {
                    Log.e(LOG_TAG, "caught " + ex);
                    mXmppSession = null;

Download the

UPDATE (11:22 AM on Nov 23, 2007) : Please see Totally *Unofficial* Android GTalk Client (Send/Receive XMPP Messages) for the follow up post/code that displays the conversation as well


  1. Great stuff! They are even using Java code. Must buy one…

    Comment by Mark — November 15, 2007 @ 12:33 pm

  2. Yes, fantastic job. I like the fact that you are not dependent on Eclipse IDE. Thank You

    Comment by Steve — November 15, 2007 @ 6:28 pm

  3. […] Sending XMPP messages in Android – Standalone sample (Ant build.xml/intellij project) Set up your XMPP Settings under Dev Tools [image] Download the and install the app adb install […] […]

    Pingback by Top Posts « — November 15, 2007 @ 7:01 pm

  4. hiii can u explain a little bit more???

    hw can we use Xmpp?
    Wat do we need ??

    please do help.


    Comment by chakradhar — November 17, 2007 @ 2:59 pm

  5. […] Простенький пример, как отправить сообщение через XMPP. Исходный код и авторы примера находятся тут: […]

    Pingback by Googlephone X Team » Blog Archive » Отправка XMPP сообщения — November 20, 2007 @ 12:16 am

  6. Thnks a lot for the information.. Hey 1 doubt

    sendTextMessage () will send a message to a gtalk user, but if we get a reply for the message we send, will there be an event fire’d. Where do we get it? can you please please post an example for it

    thanks a lot

    Comment by Amith GC — November 20, 2007 @ 7:39 am

  7. […] vous laisse consulter son billet sur le […]

    Pingback by Android After All » Envoyer du texte via XMPP — November 21, 2007 @ 11:06 am

  8. […] “voir le code”, c’est par là […]

    Pingback by Android France » Archive du blog » Comment envoyer des messages XMPP avec Android ? — November 21, 2007 @ 12:23 pm

  9. great example, thanks!

    Comment by milan — November 22, 2007 @ 10:34 am

  10. I try to receive messages as well but no success, could you post another example, if you know how to do it, please,

    Comment by milan — November 23, 2007 @ 3:20 am

  11. […] and go into Dev Tools/XMPP Settings and add a valid gmail account, then it works as expected, see here. This doesn’t seem to allow general peer to peer connections between apps and in particular […]

    Pingback by Wink Saville’s Blog » Android -Xmpp — December 21, 2007 @ 4:54 pm

  12. […] with sending XMPP message from andoird have a look at the samples in the google documentation or this post.To handle the received messages on the server end we will use a custom smack extension. The code […]

    Pingback by Android on Smack « The Tech Gene, Craig Baker — January 3, 2008 @ 3:41 am

  13. Where is the code?
    Can someone suggest?


    Comment by trackback — October 25, 2008 @ 10:43 pm

  14. Hi.. EveryOne

    I cant download the zip file.. any body have please send to me..

    Comment by Bose Pandian — February 10, 2009 @ 9:54 am

  15. Without java code it would have not become good choice.

    Comment by Sample messages — July 7, 2010 @ 4:22 am

  16. can i get a new link for because I get this message “The requested URL /~dims/android/ was not found on this server.”


    Comment by cheosite — January 29, 2012 @ 9:50 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at


Get every new post delivered to your Inbox.

Join 135 other followers

%d bloggers like this: