Android基于XMPP的即时通讯1-基本对话
闲暇之余,自己写了个简单的即时通讯,基于OpenFire服务器平台。
整个项目包括两个部分,一个是服务器端,一个是android手机端;
一、关于服务器端没什么好说的,下载安装配置即可
推荐下载带jar的程序安装,配置好java_jdk的可以使用压缩包解压安装
OpenFire下载地址:http://www.igniterealtime.org/downloads/index.jsp
顺便下载一个Spark,电脑端通讯,用于和你的安卓手机互发信息。
运行openFire截图
openfire的后台管理界面:
二、重点介绍一下android端的程序
1、首先要连接到OpenFire服务器,需要一个登陆界面,像这样

连接服务器的代码:
Thread thread = new Thread(new Runnable() {
public void run() {
try {
// 连接服务器
XmppConnection.getConnection().login(userStr, passStr);
// 连接服务器成功,更改在线状态
Presence presence = new Presence(Presence.Type.available);
XmppConnection.getConnection().sendPacket(presence);
handler.sendEmptyMessage(1);
} catch (XMPPException e) {
XmppConnection.closeConnection();
handler.sendEmptyMessage(2);
}
}
});
thread.start();
XmppConnection类的核心代码
;// 端口
public static String SERVER_HOST = "192.168.1.169";// 服务器地址
public static String SERVER_NAME = "@8nqa3d40s88hspl";// 服务器名称
private static XMPPConnection connection;
private static FileTransferManager fileManager;
private static void openConnection() {
try {
if (null == connection || !connection.isAuthenticated()) {
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration conConfig = new ConnectionConfiguration(
SERVER_HOST, SERVER_PORT, SERVER_NAME);
conConfig.setReconnectionAllowed(true);
conConfig.setSendPresence(true);
conConfig.setSASLAuthenticationEnabled(true);
connection = new XMPPConnection(conConfig);
connection.connect();
configureConnection();
}
} catch (XMPPException e) {
}
}
public static XMPPConnection getConnection() {
if (connection == null) {
openConnection();
}
return connection;
}
public static void closeConnection() {
if(connection != null){
connection.disconnect();
connection = null;
}
}
2、连接到服务器之后,获取我们的好友,像这样

获取好友列表的代码:
Thread thread = new Thread(new Runnable() {
public void run() {
try {
XMPPConnection conn = XmppConnection.getConnection();
Roster roster = conn.getRoster();
friendList = new ArrayList<Map<String,String>>();
Collection<RosterEntry> entries = roster.getEntries();
HashMap<String, String> map = null;
for (RosterEntry entry : entries) {
map = new HashMap<String, String>();
map.put("User", entry.getUser());
map.put("Name", entry.getName());
friendList.add(map);
}
handler.sendEmptyMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
3、聊天界面的程序,像这样

Spark聊天界面
接受消息的代码:
private ChatManager chatMan; private Chat newchat;
// 消息监听
chatMan = XmppConnection.getConnection().getChatManager();
newchat = chatMan.createChat(toUserID, null);
chatMan.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean able) {
chat.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
// 收到来自pc服务器的消息(获取自己好友发来的信息)
if (message.getFrom().contains(toUserID)) {
if (message.getBody().length() > 0) {
// 获取用户、消息、时间、IN
String[] args = new String[] { toUserID,
message.getBody() };
// 在handler里取出来显示消息
android.os.Message msg = handler
.obtainMessage();
msg.what = 1;
msg.obj = args;
msg.sendToTarget();
}
}
}
});
}
});
发送消息的代码:
// 发送消息
String fromUserID = mAppGlobal.getName();
String dateStr = DateTimeUtils.formatDate(new Date());
chatList.add(new Msg(dateStr, fromUserID, content, "OUT"));
// 刷新适配器
mAdapter.notifyDataSetChanged();
mListView.setSelection(ListView.FOCUS_DOWN);//刷新到底部
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Message msg = new Message();
msg.setBody(content);
// 发送消息
newchat.sendMessage(msg);
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
thread.start();
etSend.setText("");
源码送上:http://files.cnblogs.com/files/pear-lemon/XmppTest.zip
Android基于XMPP的即时通讯1-基本对话的更多相关文章
- Android基于XMPP的即时通讯3-表情发送
这篇博文主要讲表情发送的一些东西. 参考:Android基于XMPP的即时通讯1-基本对话 1.准备好资源文件 采用的是emoji的表情,我打包好了,下载地址:http://files.cnblogs ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- Android基于xmpp的即时通讯应用
xmpp是一个通信协议.因为这是个开放的协议,为了节俭开发成本,很多即时应用都采用了这个协议.Android上最常用的组合asmack +openfire.Asmack是smack的android版, ...
- 【XMPP】基于XMPP的即时通讯解决方案
什么是XMPP 介绍XMPP之前,先来看看GTalk. GTalk是Google推出的IM(Instant Messaging,即时通讯)软件,类似于QQ和MSN. 从技术角度来说,GTalk与QQ和 ...
- iOS基于XMPP实现即时通讯之一、环境的搭建
移动端访问不佳,请访问我的个人博客 使用XMPP已经有一段时间了,但是一直都没深入研究过,只是使用SDK做一些简单的操作,看了许多大神的博客,自己总结一下,准备写一系列关于XMPP的使用博客,以便于自 ...
- Android基于XMPP Smack openfire 开发的聊天室
Android基于XMPP Smack openfire 开发的聊天室(一)[会议服务.聊天室列表.加入] http://blog.csdn.net/lnb333666/article/details ...
- android环境下的即时通讯
首先了解一下即时通信的概念.通过消息通道 传输消息对象,一个账号发往另外一账号,只要账号在线,可以即时获取到消息,这就是最简单的即使通讯.消息通道可由TCP/IP UDP实现.通俗讲就是把一个人要发送 ...
- 基于openfire+smack即时通讯instant message开发
前言 Java领域的即时通信的解决方案可以考虑openfire+spark+smack.当然也有其他的选择. Openfire 是基于Jabber协议(XMPP)实现的即时通信服务器端版本,目前建议使 ...
- [Python]实现XMPP协议即时通讯发送消息功能
#-*- coding: utf-8 -*- __author__ = 'tsbc' import xmpp import time #注意帐号信息,必须加@域名格式 from_user = 'che ...
随机推荐
- hdu 4044 2011北京赛区网络赛E 树形dp ****
专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...
- hdu 4039 2011成都赛区网络赛I ***
两层搜索,直接for循环就行了,还要注意不能是自己的朋友 #include<cstdio> #include<iostream> #include<algorithm&g ...
- Sizeof与Strlen的区别与联系
转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- 实现Activity刷新 (转)
目前刷新Acitivity,只想到几种方法.仅供参考,如果您有更好的方法,请赐教. 程序界面: 点击refresh view可以刷新界面,点击write content可以在EditText中自动写入 ...
- IIS 内部运行机制
ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能力以致于可以用它来构建所有类型的Web应用. 绝大多数的人只熟悉高层的框架如: WebForms 和 WebServices ...
- hdu 5833 Zhu and 772002 高斯消元
Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...
- 用js完成毫秒格式数据的日期格式化任务
后台传过来的数据 creationTime 在后台是Date类型的 毫秒转换成 05-24 月 日格式的 //获得月日得到日期oTime function getMoth(str){ var ...
- POJ 3261 后缀数组
题目链接:http://poj.org/problem?id=3261 题意:约翰注意到奶牛产奶的之类是不断变化的,虽然他不能预测从当天到下一天的变化情况但是他知道变化是有规律的,牛奶的质量由一个整数 ...
- PHP 使用 OSS 批量上传图片
<?php set_time_limit(0); // 引入自动加载类// 确保路径是否正确 require_once 'autoload.php'; // 确定参数 需要申请 $accessK ...
- wpf,图片灰化处理
private BitmapSource ToGray(BitmapSource source) { FormatConvertedBitmap re = new FormatConvertedBit ...