일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- dock
- 전자정부프레임워크
- node.js
- rowspan
- Google Map
- PLSQL
- MFC
- jQuery
- Struts
- phonegap
- ibsheet
- WebLogic
- Android
- Eclipse
- 가우스
- oracle
- 선택적조인
- sencha touch
- PHP
- GPS
- appspresso
- swingx
- JDOM
- tomcat
- iBATIS
- JSON
- Spring
- jsr 296
- Ajax
- MySQL
Archives
- Today
- Total
Where The Streets Have No Name
안드로이드용 xmpp client 본문
smack라이브러는 http://code.google.com/p/asmack/ 에서 가져온걸 사용합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="전송"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/recieve_msg"
android:background="#ffffff"
android:textColor="#050505"
android:text=""
/>
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
public class SmackAppActivity extends Activity {
Connection connection;
Chat chat;
Handler mHandler = new Handler();
String recieveMsg = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
connection = new XMPPConnection("호스트");
try {
connection.connect();
connection.login("admin", "admin");
sendMessage("수신자@호스트", "안녕하세요. SmackApp이 실행되었습니다.");
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("SmackApp", "Received message [" + message.getBody() + "] from [" + fromName + "]");
recieveMsg = message.getBody();
mHandler.post(new Runnable() {
public void run() {
TextView view = (TextView)findViewById(R.id.recieve_msg);
view.append(recieveMsg+"\r\n");
}
});
}
}
}, filter);
} catch (XMPPException e) {
Log.e("SmackApp", "smack", e);
} catch (Exception ae){
Log.e("SmackApp", "smack", ae);
}
//connection.disconnect();
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
EditText edit = (EditText)findViewById(R.id.edit);
String sendMsg = edit.getText().toString();
sendMessage("수신자@호스트", sendMsg);
}
});
TextView view = (TextView)findViewById(R.id.recieve_msg);
view.append("수신메세지:"+"\r\n");
}
private void sendMessage(String to, String message){
Message msg = new Message(to, Message.Type.chat);
msg.setBody(message);
connection.sendPacket(msg);
}
}