Android即时聊天系统-随聊App之接口实现

接口定义请参考上篇,实现接口采用的是成熟的第三方asmack包,asmack是smack的android版,而smack是一个已经相对成熟的封装xmpp协议的第三方jar包。服务器端安装openfire服务器,通过调用asmack的相关接口可以进行用户间通信。

在写代码之前先简单介绍几个常用类

1:ConnectionConfiguration  这是一个xmpp连接的配置类 可以通过 ConnectionConfiguration  imConfig = new ConnectionConfiguration(IMConfig.IMSERVER, IMConfig.IMPORT); 配置服务器的ip和接口。

2:  XMPPConnection xmpp连接管理类,通过ConnectionConfiguration 配置好相关设置后,通过imConnection = new XMPPConnection(imConfig);创建一个新的连接。

3: Roster roster相当于联系人列表,里面存储了联系人相关信息。

4:Preference  状态:分为:在线,离线,等。(4种状态)。


具体接口实现代码如下:

public class IMChat implements IMChatImpl {

	protected static final String TAG = "IMChat";
	private Context iContext;
	private static IMChat chatInstance = null;
	private ConnectionConfiguration imConfig;
	private XMPPConnection imConnection;
	private Roster roster;
	private FileTransferManager fileTransferManager;
	private ArrayList<FriendRoster> friendList;

	private RecentDao recentDao;
	private MessageDao messageDao;

	/**
	 * 保持自动重连
	 */
	static {
		try {
			Class.forName("org.jivesoftware.smack.ReconnectionManager");
		} catch (ClassNotFoundException ex) {
			// problem loading reconnection manager
		}
	}

	private IMChat(Context context) {
		iContext = context.getApplicationContext();
		initIMConnection();
	}

	private void initIMConnection() {
		recentDao = new RecentDao(iContext);
		messageDao = new MessageDao(iContext);
		imConfig = new ConnectionConfiguration(IMConfig.IMSERVER,
				IMConfig.IMPORT);
		imConfig.setCompressionEnabled(false);
		imConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
		imConfig.setReconnectionAllowed(true);
		imConfig.setSendPresence(true);
		imConfig.setDebuggerEnabled(false);
		chatConfig();
		imConnection = new XMPPConnection(imConfig);
	}

	private void chatConfig() {
		ProviderManager pm = ProviderManager.getInstance();
		/
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值