Show me the code! – By Davanum Srinivas

Web Services, Apache, Websphere, IBM, etc.

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

with 15 comments

Set up your XMPP Settings under Dev Tools

android-xmpp-settings.png

Download the XMPPSender.zip 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

android-xmpp.png

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.
                    logMessage(getText(R.string.xmpp_session_not_found));
                    return;
                }
            } catch (DeadObjectException ex) {
                Log.e(LOG_TAG, "caught " + ex);
                logMessage(getText(R.string.found_stale_xmpp_service));
            }

            mSendButton.setEnabled(true);
        }

        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;
            mSendButton.setEnabled(false);
        }
    };

Here’s the code that actually sends the message

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

                if (mXmppSession == null) {
                    logMessage(getText(R.string.xmpp_service_not_connected));
                    return;
                }

                try {
                    mXmppSession.sendTextMessage(username, 0,
                            mTextField.getText().toString());
                } catch (DeadObjectException ex) {
                    Log.e(LOG_TAG, "caught " + ex);
                    logMessage(getText(R.string.found_stale_xmpp_service));
                    mXmppSession = null;
                    bindXmppService();
                }
            }
        }
    };

Download the XMPPSender.zip

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

Written by Davanum Srinivas

November 14, 2007 at 4:53 pm

Posted in Uncategorized

15 Responses

Subscribe to comments with RSS.

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

    Mark

    November 15, 2007 at 12:33 pm

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

    Steve

    November 15, 2007 at 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 XMPPSender.zip and install the app adb install […] [...]

    Top Posts « WordPress.com

    November 15, 2007 at 7:01 pm

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

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

    please do help.

    regards
    Ck

    chakradhar

    November 17, 2007 at 2:59 pm

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

  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

    Amith GC

    November 20, 2007 at 7:39 am

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

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

  9. great example, thanks!

    milan

    November 22, 2007 at 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,

    milan

    November 23, 2007 at 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 [...]

  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 [...]

  13. Where is the code?
    Can someone suggest?

    Thanks

    trackback

    October 25, 2008 at 10:43 pm

  14. Hi.. EveryOne

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

    Bose Pandian

    February 10, 2009 at 9:54 am


Leave a Reply