Sending XMPP messages in Android - Standalone sample (Ant build.xml/intellij project)
Set up your XMPP Settings under Dev Tools
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
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
About this entry
You’re currently reading “Sending XMPP messages in Android - Standalone sample (Ant build.xml/intellij project),” an entry on Show me the code! - By Davanum Srinivas
- Published:
- 11.14.07 / 4pm
- Category:
- Uncategorized
- Tags:


13 Comments
Jump to comment form | comments rss [?] | trackback uri [?]