openfire Android学习(五)------连接断开重连

首先要创建连接监听器,用来监听连接状态,这里我写了一个类

继承了ConnectionListener,重写了里面5个方法,最重要的两个方法connectionClosed()和connectionClosedOnError()

前者为连接正常断开,后者是连接异常断开,不管是正常还是异常,我们都要监听到

这里写了一个定时器,两秒进行连接一次,注释写的也很清楚,不做太多介绍

要注意的是我把用户名和密码都存到sharePreferences中了

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.techrare.listener;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import org.jivesoftware.smack.ConnectionListener;  
  7.   
  8. import android.util.Log;  
  9.   
  10. import com.techrare.taxicall.MainActivity;  
  11. import com.techrare.utils.Utils;  
  12. import com.techrare.utils.XmppConnection;  
  13.   
  14. /** 
  15.  * 连接监听类 
  16.  *  
  17.  * @author Administrator 
  18.  *  
  19.  */  
  20. public class TaxiConnectionListener implements ConnectionListener {  
  21.     private Timer tExit;  
  22.     private String username;  
  23.     private String password;  
  24.     private int logintime = 2000;  
  25.   
  26.     @Override  
  27.     public void connectionClosed() {  
  28.         Log.i("TaxiConnectionListener""連接關閉");  
  29.         // 關閉連接  
  30.         XmppConnection.getInstance().closeConnection();  
  31.         // 重连服务器  
  32.         tExit = new Timer();  
  33.         tExit.schedule(new timetask(), logintime);  
  34.     }  
  35.   
  36.     @Override  
  37.     public void connectionClosedOnError(Exception e) {  
  38.         Log.i("TaxiConnectionListener""連接關閉異常");  
  39.         // 判斷為帳號已被登錄  
  40.         boolean error = e.getMessage().equals("stream:error (conflict)");  
  41.         if (!error) {  
  42.             // 關閉連接  
  43.             XmppConnection.getInstance().closeConnection();  
  44.             // 重连服务器  
  45.             tExit = new Timer();  
  46.             tExit.schedule(new timetask(), logintime);  
  47.         }  
  48.     }  
  49.   
  50.     class timetask extends TimerTask {  
  51.         @Override  
  52.         public void run() {  
  53.             username = Utils.getInstance().getSharedPreferences("taxicall",  
  54.                     "account", MainActivity.context);  
  55.             password = Utils.getInstance().getSharedPreferences("taxicall",  
  56.                     "password", MainActivity.context);  
  57.             if (username != null && password != null) {  
  58.                 Log.i("TaxiConnectionListener""嘗試登錄");  
  59.                 // 连接服务器  
  60.                 if (XmppConnection.getInstance().login(username, password)) {  
  61.                     Log.i("TaxiConnectionListener""登錄成功");  
  62.                 } else {  
  63.                     Log.i("TaxiConnectionListener""重新登錄");  
  64.                     tExit.schedule(new timetask(), logintime);  
  65.                 }  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     @Override  
  71.     public void reconnectingIn(int arg0) {  
  72.     }  
  73.   
  74.     @Override  
  75.     public void reconnectionFailed(Exception arg0) {  
  76.     }  
  77.   
  78.     @Override  
  79.     public void reconnectionSuccessful() {  
  80.     }  
  81.   
  82. }  

其次就是给连接设置监听器了,最好放在登录方法里,关闭连接方法里移除监听

[java]  view plain copy
  1. // 添加連接監聽  
  2. TaxiConnectionListener connectionListener = new TaxiConnectionListener();  
  3. getConnection().addConnectionListener(connectionListener);  
[java]  view plain copy
  1. connection.removeConnectionListener(connectionListener);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值