openfire+asmack搭建的安卓即时通讯(一) 15.4.7

时间:2023-03-09 05:53:28
openfire+asmack搭建的安卓即时通讯(一) 15.4.7

  最进开始做一些android的项目,除了一个新闻客户端的搭建,还需要一个实现一个即时通讯的功能,参考了很多大神成型的实例,了解到operfire+asmack是搭建简易即时通讯比较方便,所以就写了这篇博客。

一、基础知识(这是复制别人的)

XMPP协议简介

XMPP协议(Extensible Messaging and PresenceProtocol,可扩展消息处理现场协议)是一种基于XML的协议,目的是为了解决及时通信标准而提出来的,最早是在Jabber上实现的。它继承了在XML环境中灵活的发展性。因此,基于XMPP的应用具有超强的可扩展性。并且XML很易穿过防火墙,所以用XMPP构建的应用不易受到防火墙的阻碍。利用XMPP作为通用的传输机制,不同组织内的不同应用都可以进行有效的通信。

这篇文章有基本的介绍,http://blog.****.net/xutaozero21/article/details/4873439

IM

Instant Messenger,及时通信软件,就是大家使用的QQ、MSN Messenger和Gtalk等等。其中Gtalk 就是基于XMPP 协议的一个实现,其他的则不是。当前IM 几乎作为每个上网者必然使用的工具,在国外的大型企业中有一些企业级的IM应用,但是其商业价值还没完全发挥出来。设想既然XMPP 协议是一个公开的协议,那么每个企业都可以利用它来开发适合本身企业工作,提高自身生产效率的IM;甚至,你还可以在网络游戏中集成这种通信软件,不但让你可以边游戏边聊天,也可以开发出适合游戏本身的IM 应用,比如说一些游戏关键场景提醒功能,团队语音交流等等都可以基于IM来实现。

本文主要讲解在android使用xmpp协议进行即时通信,所涉及3个主要的东西,它们是openfire、smack和spark,这个三个东东结合起来就是完整的xmpp IM实现,这里简单介绍一下这3个东东在下文的作用:

openfire主要是作为服务器,负责管理客户端的通信连接,以及提供客户端一些通信信息和连接信息。

Smack主要是xmpp协议的实现,提供了一套很好的api,所以下面操作xmpp都是通过使用smack的api来实现,当然因为是在android里,所以使用的是asmack这个包,里面方法跟smack包差不多。

Spark 是IM客户端的实现,其实就是使用了smack 的api实现的。

Openfire的安装和配置:

可参考http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html

 二、实现第一步登录

    openfire+asmack搭建的安卓即时通讯(一) 15.4.7openfire+asmack搭建的安卓即时通讯(一) 15.4.7

做好了一系列的前期准备之后我们就能看到这样的用户后台管理了,其中注册了两个用户用来测试。

1.首先可以搭建简易的样式:

openfire+asmack搭建的安卓即时通讯(一) 15.4.7够丑吧=-=

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView
android:id="@+id/textview"
android:text="@string/hello_world"
android:textSize="48dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_below="@id/textview"
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Name:"
/>
<EditText
android:id="@+id/login_name"
android:hint="Input your Name "
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/login_password"
android:hint="Input your Password "
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/buttonlogin"
android:layout_below="@id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="login in"/>
</RelativeLayout>

一个标题,两个输入,一个Login in的按钮够简单吧!!

2.代码实现:

首先将一些要用的方法封装到工具类里。

 public class connect {
private static ConnectionConfiguration connConfig;
private static XMPPConnection con; public static XMPPConnection getConnection() {//获取连接
if (con == null || !con.isConnected()) {
openConnection();
}
return con;
}
public static boolean openConnection() {//连接方法
try {
connConfig = new ConnectionConfiguration("192.168.252.1", 5222); //5222是客户端和服务端的连接端口,其中的ip是我的内网ip
// 设置登录状态为离线
connConfig.setSendPresence(false);
// 断网重连
connConfig.setReconnectionAllowed(true);
con = new XMPPConnection(connConfig);
con.connect();
return true;
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
public static void closeConnection() {//断开连接
con.disconnect();
}
     public static boolean login(String account, String password) {
try {
if (connect.getConnection() == null)
return false;
/** 登录 */
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connect.getConnection().login(account, password);
// 设置登录状态:在线
// Presence presence = new Presence(Presence.Type.available);
// connect.getConnection().sendPacket(presence);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
 public class MainActivity extends ActionBarActivity implements View.OnClickListener {//主函数
public static final int FAIL_CON=0;
public static final int SUCC_CON=0;
private String Tag = "MainAcy";
private String account;
private String pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);      //绑定按钮
findViewById(R.id.buttonlogin).setOnClickListener(this); } private android.os.Handler insHandler = new android.os.Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
// 登录成功
case 1:
Toast.makeText(MainActivity.this,"SUCCESS",Toast.LENGTH_SHORT).show();
Log.d(Tag, "login suc");
              //跳转页面
Intent intent=new Intent();
intent.putExtra("usename", account);
              //传值
intent.setClass(MainActivity.this, useractivity.class);
startActivity(intent);
break;
case 0:
Toast.makeText(MainActivity.this,"FAIL",Toast.LENGTH_SHORT).show();
Log.d(Tag, "login fail");
accountLogin();
default:
break;
}
}
}; private void accountLogin() {
new Thread() {
public void run() {
account = ((EditText) findViewById(R.id.login_name))
.getText().toString();
pwd = ((EditText) findViewById(R.id.login_password)).getText()
.toString();
boolean is = ConnecMethod.login(account, pwd);
if (is) {
insHandler.sendEmptyMessage(1);
// 保存用户名
user.UserName = account;
} else {
insHandler.sendEmptyMessage(0);
}
};
}.start();
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.buttonlogin:
accountLogin();
default:
break;
}
}
}

我们为下一个页面传递了一个值就是我们登陆的用户名,所以在用户页面要:

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.useractivity);
Intent intent =getIntent();
String username=intent.getStringExtra("usename");//接收用户名
TextView textView= (TextView) findViewById(R.id.username);再体现在用户页的一个textiew里
textView.setText(username);
connect.closeConnection();
      //使用的连接服务要在登录成功后,即时销毁,否则下一次就会登陆错误。
}

这就是第一步的完成,近期不忙就会做下一步的完成。