Chinaunix首页 | 论坛 | 博客
  • 博客访问: 417475
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-03-25 15:33:00

以一个安卓程序为例


MainActivity.java

  1. package cn.com.xiebiao.mytest;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;

  8. import org.jivesoftware.smack.AccountManager;
  9. import org.jivesoftware.smack.Chat;
  10. import org.jivesoftware.smack.ChatManager;
  11. import org.jivesoftware.smack.ChatManagerListener;
  12. import org.jivesoftware.smack.ConnectionConfiguration;
  13. import org.jivesoftware.smack.MessageListener;
  14. import org.jivesoftware.smack.XMPPConnection;
  15. import org.jivesoftware.smack.XMPPException;
  16. import org.jivesoftware.smack.packet.Message;
  17. import org.jivesoftware.smack.packet.Presence;

  18. public class MainActivity extends Activity {
  19.     private EditText editText;
  20.     private EditText editText2;
  21.     private Button button;

  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);

  26.         editText=(EditText)this.findViewById(R.id.editText);
  27.         editText2=(EditText)this.findViewById(R.id.editText2);
  28.         button=(Button)this.findViewById(R.id.button);

  29.         //hander接收监听器线程回传的数据
  30.         final Handler handler=new Handler(){
  31.             @Override
  32.             public void handleMessage(android.os.Message msg) {
  33.                 Bundle bundle=msg.getData();
  34.                 String message=bundle.getString("message");
  35.                 editText2.setText(message);
  36.             }
  37.         };

  38.         //启动通信线程
  39.         new Thread(new Runnable() {
  40.             @Override
  41.             public void run() {
  42.                 try {
  43.                     //允许调试
  44.                     XMPPConnection.DEBUG_ENABLED=true;
  45.                     //账户管理类
  46.                     AccountManager accountManager;
  47.                     //连接配置
  48.                     ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("192.168.1.102", 5222,"xiebiao.com.cn");
  49.                     connectionConfiguration.setReconnectionAllowed(true);
  50.                     connectionConfiguration.setSendPresence(true);
  51.                     //建立XMPP连接
  52.                     XMPPConnection xmppConnection = new XMPPConnection(connectionConfiguration);
  53.                     xmppConnection.connect();
  54.                     //登录
  55.                     xmppConnection.login("liulijuan", "123456");

  56.                     System.out.println("用户:"+xmppConnection.getUser()+"登录成功");

  57.                     //登录状态
  58.                     Presence presence=new Presence(Presence.Type.available);
  59.                     xmppConnection.sendPacket(presence);

  60.                     //获取聊天管理对象
  61.                     ChatManager chatManager=xmppConnection.getChatManager();

  62.                     //添加监听器,接收来自服务器的转发过来的消息,是条独立的线程,所以必须通过线程间通讯的机制将message传递给UI主线程
  63.                     chatManager.addChatListener(new ChatManagerListener() {
  64.                         @Override
  65.                         public void chatCreated(Chat chat, final boolean b) {

  66.                             chat.addMessageListener(new MessageListener() {
  67.                                 @Override
  68.                                 public void processMessage(Chat chat, Message message) {

  69.                                     System.out.println("--->>收到来自"+message.getFrom() + "的消息:"+message.getBody());

  70.                                     //将消息传给UI主线程更新数据
  71.                                     android.os.Message message1=handler.obtainMessage();
  72.                                     Bundle bundle=new Bundle();
  73.                                     bundle.putString("message",message.getBody());
  74.                                     message1.setData(bundle);
  75.                                     message1.sendToTarget();
  76.                                     editText2.setText(message.getBody());
  77.                                 }
  78.                             });
  79.                         }
  80.                     });

  81.                     //一个chat是一条线程,createChat()的第二个参数为一个MessageListener,我们设为null
  82.                     final Chat chat=chatManager.createChat("admin@xiebiao.com.cn",null);

  83.                     //button监听器属于主线程,所以editText.setText()有效
  84.                     button.setOnClickListener(new View.OnClickListener() {
  85.                         @Override
  86.                         public void onClick(View view) {
  87.                             String message = editText.getText().toString();
  88.                             try {
  89.                                 chat.sendMessage(message);
  90.                                 editText.setText("");
  91.                             } catch (XMPPException e) {
  92.                                 e.printStackTrace();
  93.                             }
  94.                         }
  95.                     });
  96.                 } catch (XMPPException e) {
  97.                     e.printStackTrace();
  98.                 }
  99.             }
  100.         }).start();
  101.     }

activity_main.xml

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools="" android:layout_width="match_parent"
  3.     android:layout_height="match_parent">
  4.     <LinearLayout
  5.         android:layout_width="match_parent"
  6.         android:layout_height="wrap_content"
  7.         android:id="@+id/linearLayout">
  8.     <TextView
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="发送:"
  12.         android:textSize="25sp"
  13.         android:id="@+id/textView"
  14.         android:layout_alignParentTop="true"
  15.         android:layout_alignParentLeft="true"
  16.         android:layout_alignParentStart="true" />

  17.         <EditText
  18.             android:layout_width="match_parent"
  19.             android:layout_height="wrap_content"
  20.             android:id="@+id/editText"
  21.             android:textSize="25sp"/>
  22.     </LinearLayout>

  23.     <Button
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:text="发送"
  27.         android:id="@+id/button"
  28.         android:layout_below="@+id/linearLayout"
  29.         android:layout_centerHorizontal="true" />

  30.     <LinearLayout
  31.         android:layout_width="match_parent"
  32.         android:layout_height="wrap_content"
  33.         android:layout_below="@+id/button">

  34.         <TextView
  35.             android:layout_width="wrap_content"
  36.             android:layout_height="wrap_content"
  37.             android:text="接收:"
  38.             android:textSize="25sp"
  39.             android:id="@+id/textView2"
  40.             android:layout_below="@+id/button"

  41.              />

  42.         <EditText
  43.             android:layout_width="wrap_content"
  44.             android:layout_height="wrap_content"
  45.             android:id="@+id/editText2"
  46.             android:layout_weight="1" />


  47.     </LinearLayout>


  48. </RelativeLayout>
另外在AndroidMainfest.xml中添加网络权限

阅读(1707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~