Smack 中 ConnectionListener 的作用和使用
来源:互联网 发布:新店淘宝客推广 编辑:程序博客网 时间:2024/06/16 15:34
一、简介
ConnectionListener 接口提供了对 connection 关闭相关事件的监听。
包括:
1、连接已经关闭 connectionClosed()
2、连接因为异常关闭 connectionClosedOnError()
3、在指定秒数后重新连接 reconnectingIn(int timeSecond)
4、重新连接成功 reconnectionSuccessful()
5、重新连接失败 reconnectionFailed()
所有实现了 ConnectionListener 接口并实现响应方法的类,
通过 connection.addConnectionListener(ConnectionListener connectionListener) 加入监听队列的监听器都会被调用。
二、使用方法
如果业务需要对连接的这些行为做处理,就可以按照下列步骤:
1、创建新的监听器:
ConnectionListener connListener = new ConnectionListener() {public void connectionClosed() {Log.d("SMACK", dateFormatter.format(new Date()) + " Connection closed (" + connection.hashCode() + ")");}public void connectionClosedOnError(Exception e) {Log.d("SMACK", dateFormatter.format(new Date()) + " Connection closed due to an exception (" + connection.hashCode() + ")");e.printStackTrace();}public void reconnectionFailed(Exception e) {Log.d("SMACK", dateFormatter.format(new Date()) + " Reconnection failed due to an exception (" + connection.hashCode() + ")");e.printStackTrace();}public void reconnectionSuccessful() {Log.d("SMACK", dateFormatter.format(new Date()) + " Connection reconnected (" + connection.hashCode() + ")");}public void reconnectingIn(int seconds) {Log.d("SMACK", dateFormatter.format(new Date()) + " Connection (" + connection.hashCode() + ") will reconnect in " + seconds);}};
incopy
- 2、添加到connection connection.addConnectionListener(connListener);
三、调用时机
1、packetReader.shutdown()
- for (ConnectionListener listener : connection.getConnectionListeners()) {
- try {
- listener.connectionClosed();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
2、connection.connect() 成功之后。
- if (connected && wasAuthenticated) {
- // Make the login
- ...
- notifyReconnection();
- }
http://blog.csdn.net/teamlet/article/details/25915731
0 0
- Smack 中 ConnectionListener 的作用和使用
- Smack 中 ConnectionListener 的作用和使用
- Smack 中 PacketCollector的作用和用法举例
- Smack 中 PacketCollector的作用和用法举例
- androidpn和smack的关系与使用上的差别
- Super Smack安装和使用
- Android中requestfocus的作用和使用
- smack中ProviderManager的学习
- Smack XMPPConnection 的简单使用
- 【openfire,smack使用总结】--Smack库的使用
- smack类库介绍和使用
- Html中Label标记的作用和使用介绍
- XCode 中使用-Objc和-all_load的作用
- JSP中 Session和作用域的使用
- JSP中 Session和作用域的使用
- java编程中Properties类的具体作用和使用
- Smack中IQProvider的一个问题。
- #define中#和##的作用
- ZooKeeper伪分布式集群安装及使用
- Tomcat实现的功能
- 保障性住房
- CSS3 圆角制作的消息提示图标
- LigerUI学习---界面搭建、数据请求、分页实现(持续跟新)
- Smack 中 ConnectionListener 的作用和使用
- jquery获取所有选中的checkbox
- nyoj.255 C小加 之 随机数【水题】 20141125
- android 通过wifi 获取经纬度和获取渠道号/屏幕宽高
- Hello object-c!!
- redis java客户端Jedis 连接池实现
- hdu-1240
- Makefile 基础使用总结
- 第十三周项目六:体验文件操作5.0