http://blog.csdn.net/ming_light/article/category/1531703


asmack本身封装了断线重连机制,但是有一点需要注意

在设置链接属性钱,要加载ReconnectionManager这个类,否则没有效果。


直接上代码:

第一步

[java]  view plain copy
  1. try {  
  2.                     Class.forName("org.jivesoftware.smack.ReconnectionManager");  
  3.                 } catch (Exception e1) {  
  4.                 }    

第二步   设置链接参数,启动重连功能

[java]  view plain copy
  1. ConnectionConfiguration configuration = new ConnectionConfiguration(  
  2.                 Contents.HOST, Contents.PORT);  
  3.         configuration.setReconnectionAllowed(true);  

第三步   链接后注册监听

[java]  view plain copy
  1. connection.connect();  
  2. if (connection.isConnected()) {  
  3.                           
  4. connection.addConnectionListener(connectionListener);  
  5.                           
  6.                     }  
  7.   
  8.   
  9. public static ConnectionListener connectionListener = new ConnectionListener() {  
  10.   
  11.         @Override  
  12.         public void reconnectionSuccessful() {  
  13.             Log.i("connection""reconnectionSuccessful");  
  14.         }  
  15.   
  16.         @Override  
  17.         public void reconnectionFailed(Exception arg0) {  
  18.             Log.i("connection""reconnectionFailed");  
  19.         }  
  20.   
  21.         @Override  
  22.         public void reconnectingIn(int arg0) {  
  23.             Log.i("connection""reconnectingIn");  
  24.         }  
  25.   
  26.         @Override  
  27.         public void connectionClosedOnError(Exception arg0) {  
  28.             Log.i("connection""connectionClosedOnError");  
  29.               
  30.         } 

更多相关文章

  1. android系统属性获取及设置
  2. Android(安卓)多次点击的另一种思路
  3. Android实现标题显示隐藏功能
  4. [置顶] Android中遇到的需求与解决方案一
  5. android 中如何监听耳机键消息
  6. [转]Android中Spannable的使用
  7. Android(安卓)Studio通过配置build.gradle参数设置全局的Log开关
  8. android 8.0 (API=26) 升级适配遇到的两个问题
  9. Android(安卓)如何设置默认语言

随机推荐

  1. # Android: ListView 学生管理
  2. android设备唯一码的获取,cpu号,mac地址
  3. eoe android客户端源码剖析(二)侧滑菜单Sli
  4. Retrofit 2.0 超能实践(一),okHttp完美支持H
  5. Android(安卓)JUnit test 进行自动化测试
  6. Android(安卓)USB 主机(Host)和配件(Accesso
  7. Android studio如何集成ShareSDK详解
  8. 提高Android在eclipse下的编译速度
  9. Windows下搭建android开发环境
  10. Android_android的测试工具CTS