Here is the screen shot
Functionality
- Enter the recipient’s name in the edit box on top and press “setup” button
- Enter the message text in the edit box on the bottom and press “send” button to send the message to the recipient
Source
package org.apache.gtalk;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.provider.Im;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.google.android.xmppService.IXmppService;
import com.google.android.xmppService.IXmppSession;
import com.google.android.xmppService.Presence;
public class GTalkClient extends Activity implements View.OnClickListener {
private static final String LOG_TAG = "GTalkClient";
IXmppSession mXmppSession = null;
EditText mSendText;
ListView mListMessages;
EditText mRecipient;
Button mSend;
Button mSetup;
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// gather the troops
mSendText = (EditText) findViewById(R.id.sendText);
mListMessages = (ListView) findViewById(R.id.listMessages);
mRecipient = (EditText) findViewById(R.id.recipient);
mSend = (Button) findViewById(R.id.send);
mSetup = (Button) findViewById(R.id.setup);
// set up handler for on click
mSetup.setOnClickListener(this);
mSend.setOnClickListener(this);
bindService((new Intent()).setComponent(
com.google.android.xmppService.XmppConstants.XMPP_SERVICE_COMPONENT),
null, mConnection, 0);
}
/**
* Let the user know there was an issue
*
* @param msg
*/
private void logMessage(CharSequence msg) {
NotificationManager nm = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
nm.notifyWithText(123, msg, NotificationManager.LENGTH_LONG, null);
}
/**
* Setup the XMPP Session using a service connection
*/
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;
}
mXmppSession.setPresence(new Presence(Im.PresenceColumns.AVAILABLE, "Am here now!"));
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, "caught " + ex);
logMessage(getText(R.string.found_stale_xmpp_service));
}
mSendText.setEnabled(true);
}
public void onServiceDisconnected(ComponentName componentName) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mXmppSession = null;
mSendText.setEnabled(false);
}
};
/**
* Handle clicks on the 2 buttions
*
* @param view
*/
public void onClick(View view) {
if (view == mSetup) {
Log.i(LOG_TAG, "onClick - Setup");
// Run a query against CONTENT_URI = "content://im/messages"
Cursor cursor = managedQuery(Im.Messages.CONTENT_URI, null,
"contact=\'" + mRecipient.getText().toString() + "\'", null, null);
// Display the cursor results in a simple list
// Note that the adapter is dyamic (picks up new entries automatically)
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor, // Give the cursor to the list adatper
new String[]{Im.MessagesColumns.BODY},
new int[]{android.R.id.text1});
this.mListMessages.setAdapter(adapter);
} else if (view == mSend) {
// use XmppService to send data message to someone
String username = mRecipient.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, mSendText.getText().toString());
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, "caught " + ex);
logMessage(getText(R.string.found_stale_xmpp_service));
mXmppSession = null;
}
}
}
private boolean isValidUsername(String username) {
return !TextUtils.isEmpty(username) && username.indexOf('@') != -1;
}
}
Download the sources and Application – GTalkClient.zip
[...] 11.23 Totally *Unofficial* Android GTalk Client (Send/Receive XMPP Messages) [...]
Sending XMPP messages in Android - Standalone sample (Ant build.xml/intellij project) « Show me the code!
November 23, 2007 at 11:26 am
[...] site “Show me the code !” continue sur sa lancée et nous propose aujourd’hui un client GTalk pour Android qui fonctionne grâce à l’envoi et la réception de messages [...]
Android : Client GTalk
November 23, 2007 at 12:20 pm
[...] with Android this week, releasing new source code on an almost daily basis. Today he releases a *totally* unofficial Gtalk client for Android. He also includes source code for sending and receiving XMPP messages and authenticating with [...]
Source Code: A Gtalk Client from Davanum | Android Notes
November 23, 2007 at 12:36 pm
Great App. A way to learn more about android. Something I planned doing when I get a firm grasp of it. Anyway, great effort.
Henry Addo
November 23, 2007 at 5:37 pm
[...] and download *Unoficial* Android’ GTalk Client Application here. Hurry [...]
gPhone Daily » Blog Archive » Android GTalk client application
November 23, 2007 at 7:50 pm
Last week a gave a quick look at XMPP support in Android and getting the service was quite simple, but I didn’t understand how to receive plain messages (p2p data messages are received as intents). Where did you find info about the android.provider.Im package? In the documentation there is nothing about it, and google is of no help.
Fabio Forno
November 24, 2007 at 4:41 pm
[...] Totally *Unofficial* Android GTalk Client (Send/Receive XMPP Messages) Here is the screen shot [image] [...]
Top Posts « WordPress.com
November 24, 2007 at 7:01 pm
[...] Show me the code! via gPhone [...]
Google Talk using unnoficial Android GTalk client application — 3GWeek
November 24, 2007 at 9:23 pm
Thanks!!!
milan
November 25, 2007 at 9:20 am
What moment do you detect the incoming message in?
Quauhtli Martinez
November 26, 2007 at 10:18 pm
How do you delete the messages that have been read for an specific user?
Quauhtli Martinez
November 27, 2007 at 11:19 am
[...] projets simples mais intéressant et fonctionnels. On peux citer en vrac son Client Twitter, son Client GTalk, son utilitaire de GéoLocalisation sans GPS et bien d’autre que nous vous laissons [...]
» Le développeur du mois
December 2, 2007 at 1:34 pm
It is not actually recieve messages. It prints jsut sent one. Do you know a way to actually recieve messages?
simeon
December 16, 2007 at 7:24 pm
Simeon,
It does show both received and sent message. Just that you need to scroll, they are at the end.
The in message is detected as soon as it arrives.
I added a line mSendText.setText(”"); after
mXmppSession.sendTextMessage(username, 0, mSendText.getText().toString());
Would be nice if the end of few lines from the list adapter show on the screen, once the screen is full you have to scroll the list down to see the latest messages. (other alternative is to set your query so that it will show you the latest message first. (in reverse order))
This way you don’t have to erase the previous message to sent.
Thanks.
jsyadav
December 30, 2007 at 12:19 am
Simeon,
It does show both received and sent message. Just that you need to scroll, they are at the end.
The in message is detected as soon as it arrives.
I added a line mSendText.setText(”"); after
mXmppSession.sendTextMessage(username, 0, mSendText.getText().toString());
This way you don’t have to erase the previous message to sent.
Would be nice if the end of few lines from the list adapter show on the screen, once the screen is full you have to scroll the list down to see the latest messages. (other alternative is to set your query so that it will show you the latest message first. (in reverse order))
Thanks.
jsyadav
December 30, 2007 at 12:21 am
Very cool article, and useful. Keep up the good work.
Dipankar Sarkar
January 6, 2008 at 1:19 pm
hi,
The application work perfect, if I just need to sent the messages. But I want to do some processing on the received messages how can I get last received message.
Regards
Prashant
January 18, 2008 at 12:15 am
Very cool article, and useful. Keep up the good work.
kimochi
January 19, 2008 at 4:46 pm
Hi ,
I am new to android .
How we can invoke web services on remote server through android.
Any help?
by
vamsi
vamsi
January 25, 2008 at 3:32 am
i see no message came in?
please help
is it really can receive text message from gtalk client?
enshrentko
January 26, 2008 at 11:42 am
Hi, just read this post. Thank you!
Serg
February 24, 2008 at 10:30 am
Hello!
I have used this code, but there is a problem due to the change of version of the sdk. I have changed all xmpp appearences for gtalk. But i don´t know how to update this two calls:
private void logMessage(CharSequence msg) {
NotificationManager nm = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
nm.notifyWithText(123, msg, NotificationManager.LENGTH_LONG, null);
}
In the new sdk notificationmanager doesn´t work this way.
And the other problem:
mXmppSession.sendTextMessage(username, 0, mSendText.getText().toString());
Now we can´t use sendTextMessage, we have to use sendDataMessage(String to, Intent broadcastintent)
How is sendDataMessage(…) used? do we have to create a new class just to write the text message in the receipent??
thank you very much!!
xayide
February 28, 2008 at 1:46 pm
its the best post from you, thanks a lot
SamaraRegion
February 28, 2008 at 10:56 pm
Hi,
I’m getting problem to import import
import com.google.android.xmppService
I’m using eclipse platform. Any hint?
Baishakhi
April 6, 2008 at 3:44 pm
[…]TVery cool article, and useful. Keep up the good work[…]
photoshop
May 18, 2008 at 1:25 pm
Would be nice if the end of few lines from the list adapter show on the screen, once the screen is full you have to scroll the list down to see the latest messages.
SEO collection links on optimization competitions
June 23, 2008 at 9:09 am
Nice work! This weekend i’ll try your source. Thanx a lot.
Бизнес идеи
January 2, 2009 at 3:44 pm
Hi,
Thenks for the blog!
Все для женщин
January 13, 2009 at 4:50 pm
Dont cheat me..
I’m not able to download the zip file from this page.
give the file to download..
Bose Pandian
February 19, 2009 at 5:58 am