http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/
Android – Just Use Smack API For XMPP
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; |
02 |
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; |
12 |
13 |
/** |
14 |
* Gather the xmpp settings and create an XMPPConnection |
15 |
*/ |
16 |
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener { |
17 |
private XMPPClient xmppClient; |
18 |
19 |
public SettingsDialog(XMPPClient xmppClient) { |
20 |
super (xmppClient); |
21 |
this .xmppClient = xmppClient; |
22 |
} |
23 |
24 |
protected void onStart() { |
25 |
super .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 ); |
31 |
} |
32 |
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); |
39 |
40 |
// Create a connection |
41 |
ConnectionConfiguration connConfig = |
42 |
new ConnectionConfiguration(host, Integer.parseInt(port), service); |
43 |
XMPPConnection connection = new XMPPConnection(connConfig); |
44 |
45 |
try { |
46 |
connection.connect(); |
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 ); |
51 |
} |
52 |
try { |
53 |
connection.login(username, password); |
54 |
Log.i( "XMPPClient" , "Logged in as " + connection.getUser()); |
55 |
56 |
// Set the status to available |
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 ); |
63 |
} |
64 |
dismiss(); |
65 |
} |
66 |
67 |
private String getText( int id) { |
68 |
EditText widget = (EditText) this .findViewById(id); |
69 |
return widget.getText().toString(); |
70 |
} |
71 |
} |
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; |
002 |
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; |
019 |
020 |
import java.util.ArrayList; |
021 |
022 |
public class XMPPClient extends Activity { |
023 |
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; |
031 |
032 |
/** |
033 |
* Called with the activity is first created. |
034 |
*/ |
035 |
@Override |
036 |
public void onCreate(Bundle icicle) { |
037 |
super .onCreate(icicle); |
038 |
setContentView(R.layout.main); |
039 |
040 |
mRecipient = (EditText) this .findViewById(R.id.recipient); |
041 |
mSendText = (EditText) this .findViewById(R.id.sendText); |
042 |
mList = (ListView) this .findViewById(R.id.listMessages); |
043 |
setListAdapter(); |
044 |
045 |
// Dialog for getting the xmpp settings |
046 |
mDialog = new SettingsDialog( this ); |
047 |
048 |
// Set a listener to show the settings dialog |
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() { |
053 |
public void run() { |
054 |
mDialog.show(); |
055 |
} |
056 |
}); |
057 |
} |
058 |
}); |
059 |
060 |
// Set a listener to send a chat text message |
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(); |
066 |
067 |
Log.i( "XMPPClient" , "Sending text [" + text + "] to [" + to + "]" ); |
068 |
Message msg = new Message(to, Message.Type.chat); |
069 |
msg.setBody(text); |
070 |
connection.sendPacket(msg); |
071 |
messages.add(connection.getUser() + ":" ); |
072 |
messages.add(text); |
073 |
setListAdapter(); |
074 |
} |
075 |
}); |
076 |
} |
077 |
078 |
/** |
079 |
* Called by Settings dialog when a connection is establised with the XMPP server |
080 |
* |
081 |
* @param connection |
082 |
*/ |
083 |
public void setConnection |
084 |
(XMPPConnection |
085 |
connection) { |
086 |
this .connection = connection; |
087 |
if (connection != null ) { |
088 |
// Add a packet listener to get messages sent to us |
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()); |
098 |
// Add the incoming message to the list view |
099 |
mHandler.post( new Runnable() { |
100 |
public void run() { |
101 |
setListAdapter(); |
102 |
} |
103 |
}); |
104 |
} |
105 |
} |
106 |
}, filter); |
107 |
} |
108 |
} |
109 |
110 |
private void setListAdapter |
111 |
() { |
112 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this , |
113 |
R.layout.multi_line_list_item, |
114 |
messages); |
115 |
mList.setAdapter(adapter); |
116 |
} |
117 |
} |