基于XMPP利用openfire简单的即时通讯

功能的实现结果:能够使自己编写客户端与spark客户端信息通讯,将接受到的信息更新到textview上。

1.下载openfire并安装。设置域名,添加用户

2.下载安装spark客户端

3.jar包 :asmack.jar

4.权限:

1 <uses-permission android:name="android.permission.INTERNET"/>
View Code

 

5.简单布局:

 1     <Button
 2         android:layout_width="wrap_content"
 3         android:layout_height="wrap_content"
 4         android:onClick="login"
 5         android:text="登录"
 6         android:id="@+id/button" />
 7 
 8     <Button
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:onClick="sendMsg"
12         android:text="发送消息"
13         android:id="@+id/button2"
14         android:layout_gravity="center_vertical" />
15 
16     <TextView
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:text="New Text"
20         android:id="@+id/content" />
View Code

 

6.Activity代码:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     XMPPConnection con;
 4     TextView tv;
 5     //用于刷新textview的信息text
 6     Handler hand=new Handler(){
 7         @Override
 8         public void handleMessage(android.os.Message msg) {
 9             tv.setText(msg.obj.toString());
10         }
11     };
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         init();
17         initView();
18     }
19 
20     private void initView() {
21         tv= (TextView) findViewById(R.id.content);
22     }
23 
24     private void init() {
25         new Thread(){
26             @Override
27             public void run() {
28                 try {
29                     //指定连接的ip地址和端口 以及服务器的域名
30                     ConnectionConfiguration config=new ConnectionConfiguration("10.8.167.75",5222,"gxw");
31                     //连接对象
32                     con =new XMPPConnection(config);
33                     //开启连接
34                     con.connect();
35                 } catch (Exception e) {
36                     e.printStackTrace();
37                 }
38             }
39         }.start();
40 
41     }
42     public void  login(View v){
43         try {
44             //登陆的用户账号和密码
45             con.login("bb","123456x");
46             System.out.print("");
47         } catch (Exception e) {
48             e.printStackTrace();
49         }
50     }
51     public void sendMsg(View v){
52 
53         ChatManager cm = con.getChatManager();
54         cm.addChatListener(new ChatManagerListener() {
55             @Override
56             public void chatCreated(Chat chat, boolean b) {
57                 chat.addMessageListener(new MessageListener() {
58                     @Override
59                     public void processMessage(Chat chat, Message message) {
60                         android.os.Message msg= android.os.Message.obtain();
61                         if (!TextUtils.isEmpty(message.getBody())){
62                             msg.obj=message.getBody();
63                             hand.sendMessage(msg);
64                         }
65                     }
66                 });
67             }
68         });
69 
70 
71         //aa为进行通信的账号,@后为域名
72         Chat chat = cm.createChat("aa@gxw",null);
73         try {
74             chat.sendMessage("1111111");
75         } catch (Exception e) {
76             e.printStackTrace();
77         }
78     }
79 }

 

posted @ 2016-06-01 19:35  咖喱不见不散啊  阅读(167)  评论(0编辑  收藏  举报