OUTDATED SAMPLE – Updated code is here:
http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/
Using Smack XMPP API From Android
Once you get tired of the limitations of android’s built-in IMProvider and the corresponding API – IXmppSession and IXmppService, try the sample below. Inside the source/binary zip (bottom of this article) you will find a smack.jar that works with android. To build the jar yourself, You can download the Smack 3.0.4 sources from here and apply the patch here.
Here Is A Screen Shot Of The XMPP Settings Dialog.
Notes
- For GTalk, use “gtalk.google.com” as host with port 5222. The service name is “gmail.com”
- Don’t add “@gmail.com” in the user name, just the id will do
Here’s The Code For The Settings Dialog
01 |
package org.apache.android.xmpp; |
03 |
import android.app.Dialog; |
04 |
import android.util.Log; |
05 |
import android.view.View; |
06 |
import android.widget.Button; |
07 |
import android.widget.EditText; |
08 |
import org.jivesoftware.smack.ConnectionConfiguration; |
09 |
import org.jivesoftware.smack.XMPPConnection; |
10 |
import org.jivesoftware.smack.XMPPException; |
11 |
import org.jivesoftware.smack.packet.Presence; |
14 |
* Gather the xmpp settings and create an XMPPConnection |
16 |
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener { |
17 |
private XMPPClient xmppClient; |
19 |
public SettingsDialog(XMPPClient xmppClient) { |
21 |
this .xmppClient = xmppClient; |
24 |
protected void onStart() { |
26 |
setContentView(R.layout.settings); |
27 |
getWindow().setFlags( 4 , 4 ); |
28 |
setTitle( "XMPP Settings" ); |
29 |
Button ok = (Button) findViewById(R.id.ok); |
30 |
ok.setOnClickListener( this ); |
33 |
public void onClick(View v) { |
34 |
String host = getText(R.id.host); |
35 |
String port = getText(R.id.port); |
36 |
String service = getText(R.id.service); |
37 |
String username = getText(R.id.userid); |
38 |
String password = getText(R.id.password); |
41 |
ConnectionConfiguration connConfig = |
42 |
new ConnectionConfiguration(host, Integer.parseInt(port), service); |
43 |
XMPPConnection connection = new XMPPConnection(connConfig); |
47 |
Log.i( "XMPPClient" , "[SettingsDialog] Connected to " + connection.getHost()); |
48 |
} catch (XMPPException ex) { |
49 |
Log.e( "XMPPClient" , "[SettingsDialog] Failed to connect to " + connection.getHost()); |
50 |
xmppClient.setConnection( null ); |
53 |
connection.login(username, password); |
54 |
Log.i( "XMPPClient" , "Logged in as " + connection.getUser()); |
57 |
Presence presence = new Presence(Presence.Type.available); |
58 |
connection.sendPacket(presence); |
59 |
xmppClient.setConnection(connection); |
60 |
} catch (XMPPException ex) { |
61 |
Log.e( "XMPPClient" , "[SettingsDialog] Failed to log in as " + username); |
62 |
xmppClient.setConnection( null ); |
67 |
private String getText( int id) { |
68 |
EditText widget = (EditText) this .findViewById(id); |
69 |
return widget.getText().toString(); |
Here Is A Screen Shot Of The Main Window.
Notes
- In the Recipient field, make sure you add the “@gmail.com”, not just the user id
Here’s The Code For The Main Activity
001 |
package org.apache.android.xmpp; |
003 |
import android.app.Activity; |
004 |
import android.os.Bundle; |
005 |
import android.os.Handler; |
006 |
import android.util.Log; |
007 |
import android.view.View; |
008 |
import android.widget.ArrayAdapter; |
009 |
import android.widget.Button; |
010 |
import android.widget.EditText; |
011 |
import android.widget.ListView; |
012 |
import org.jivesoftware.smack.PacketListener; |
013 |
import org.jivesoftware.smack.XMPPConnection; |
014 |
import org.jivesoftware.smack.filter.MessageTypeFilter; |
015 |
import org.jivesoftware.smack.filter.PacketFilter; |
016 |
import org.jivesoftware.smack.packet.Message; |
017 |
import org.jivesoftware.smack.packet.Packet; |
018 |
import org.jivesoftware.smack.util.StringUtils; |
020 |
import java.util.ArrayList; |
022 |
public class XMPPClient extends Activity { |
024 |
private ArrayList<String> messages = new ArrayList(); |
025 |
private Handler mHandler = new Handler(); |
026 |
private SettingsDialog mDialog; |
027 |
private EditText mRecipient; |
028 |
private EditText mSendText; |
029 |
private ListView mList; |
030 |
private XMPPConnection connection; |
033 |
* Called with the activity is first created. |
036 |
public void onCreate(Bundle icicle) { |
037 |
super .onCreate(icicle); |
038 |
setContentView(R.layout.main); |
040 |
mRecipient = (EditText) this .findViewById(R.id.recipient); |
041 |
mSendText = (EditText) this .findViewById(R.id.sendText); |
042 |
mList = (ListView) this .findViewById(R.id.listMessages); |
046 |
mDialog = new SettingsDialog( this ); |
049 |
Button setup = (Button) this .findViewById(R.id.setup); |
050 |
setup.setOnClickListener( new View.OnClickListener() { |
051 |
public void onClick(View view) { |
052 |
mHandler.post( new Runnable() { |
061 |
Button send = (Button) this .findViewById(R.id.send); |
062 |
send.setOnClickListener( new View.OnClickListener() { |
063 |
public void onClick(View view) { |
064 |
String to = mRecipient.getText().toString(); |
065 |
String text = mSendText.getText().toString(); |
067 |
Log.i( "XMPPClient" , "Sending text [" + text + "] to [" + to + "]" ); |
068 |
Message msg = new Message(to, Message.Type.chat); |
070 |
connection.sendPacket(msg); |
071 |
messages.add(connection.getUser() + ":" ); |
079 |
* Called by Settings dialog when a connection is establised with the XMPP server |
083 |
public void setConnection |
086 |
this .connection = connection; |
087 |
if (connection != null ) { |
089 |
PacketFilter filter = new MessageTypeFilter(Message.Type.chat); |
090 |
connection.addPacketListener( new PacketListener() { |
091 |
public void processPacket(Packet packet) { |
092 |
Message message = (Message) packet; |
093 |
if (message.getBody() != null ) { |
094 |
String fromName = StringUtils.parseBareAddress(message.getFrom()); |
095 |
Log.i( "XMPPClient" , "Got text [" + message.getBody() + "] from [" + fromName + "]" ); |
096 |
messages.add(fromName + ":" ); |
097 |
messages.add(message.getBody()); |
099 |
mHandler.post( new Runnable() { |
110 |
private void setListAdapter |
112 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this , |
113 |
R.layout.multi_line_list_item, |
115 |
mList.setAdapter(adapter); |
Download Source And APK From Here – XMPPClient.Zip