org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
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.
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 |
} |
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 |
} |
수신되는 패킷을 처리하는데 두가지 방식을 지원한다.
org.jivesoftware.smack.filter.PacketFilter인터페이스는 패킷리스너와 패킷콜렉터가 전달받을킷을러주는 역할을 한다. org.jivesoftware.smack.filter패키지에 미리정의된 많은 사용가능한 필터들이 정의되어 있다.
아래 코드는 패킷리스너와 패킷콜렉터를 모두 사용하는 데모이다.
// Create a packet filter to listen for new messages from a particular // user. We use an AndFilter to combine two other filters.
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class),
new FromContainsFilter("mary@jivesoftware.com"));
// Assume we've created an XMPPConnection name "connection".
// First, register a packet collector using the filter we created.
PacketCollector myCollector = connection.createPacketCollector(filter);
// Normally, you'd do something with the collector, like wait for new packets.
// Next, create a packet listener. We use an anonymous inner class for brevity.
PacketListener myListener = new PacketListener() {
public void processPacket(Packet packet) {
// Do something with the incoming packet here.
}
};
// Register the listener.
connection.addPacketListener(myListener, filter);
Standard Packet Filters
XMPPConnection connection = new XMPPConnection("jabber.org"); connection.connect(); connection.login("mtucker", "password"); Chat chat = connection.getChatManager().createChat("jsmith@jivesoftware.com", new MessageListener() { public void processMessage(Chat chat, Message message) { System.out.println("Received message: " + message); } });chat.sendMessage("Howdy!"); |
XMPP XML format이나 XML에 대해서도 알필요가 없다.