Android端使用asmack实现IM

来源:互联网 发布:微信昵称特殊字符 php 编辑:程序博客网 时间:2024/06/05 17:15

昨天简单介绍了一下 ejabberd 在Windows下的安装,然后PC端来使用 spark 连接 ejabberd,今天一捣鼓了一下asmack

在官网看到smack 4.1.0 已经支持Android了,不用再使用移植版的asmack了,不过现在是bate版 ,有兴趣的去看一下:http://www.igniterealtime.org/projects/smack/


第一步,引用 asmack-jse-buddycloud-2010.12.11.jar


下面一步一步来

1.连接服务器

                //三个参数分别为:主机IP,端口(一般都是5222),安装ejabberd时填写的的域名                ConnectionConfiguration connectionConfig = new ConnectionConfiguration("192.168.1.100", 5222, "localhost");                // 允许自动连接                connectionConfig.setReconnectionAllowed(true);                // 允许登陆成功后更新在线状态                connectionConfig.setSendPresence(true);                                //下面为解决 找不到密钥凭证,4.0+系统不同                Log.i("当前操作系统版本API Level=", Build.VERSION.SDK_INT + ""); //$NON-NLS-1$ //$NON-NLS-2$                  if (Build.VERSION.SDK_INT >= 14) {                    connectionConfig.setTruststoreType("AndroidCAStore"); //$NON-NLS-1$                      connectionConfig.setTruststorePassword(null);                    connectionConfig.setTruststorePath(null);                } else {                    connectionConfig.setTruststoreType("BKS"); //$NON-NLS-1$                      String path = System.getProperty("javax.net.ssl.trustStore"); //$NON-NLS-1$                      if (path == null)                        path = System.getProperty("java.home") + File.separator //$NON-NLS-1$                                  + "etc" + File.separator + "security" //$NON-NLS-1$ //$NON-NLS-2$                                  + File.separator + "cacerts.bks"; //$NON-NLS-1$                      connectionConfig.setTruststorePath(path);                }                                connectionConfig.setSecurityMode(SecurityMode.disabled);                                //获取到一个XMPPConnection对象                connection = new XMPPConnection(connectionConfig);                try {                    connection.connect();// 开启连接                } catch (XMPPException e) {                    throw new IllegalStateException(e);                }

获取账户管理类

 accountManager = connection.getAccountManager();// 获取账户管理类

注册用户
        try {            accountManager.createAccount(username, password);        } catch (XMPPException e) {            e.printStackTrace();        }

登录
 connection.login("用户名", "密码", "客户端标识");

创建一个聊天
注意上面监听消息的写法,smack 4.0.6的版本也是需要这样写,否则可能processMessage()不能被回调
            ChatManager chatmanager = connection.getChatManager();            newChat = chatmanager.createChat(username + "@localhost", null);            chatmanager.addChatListener(new ChatManagerListener() {                @Override                public void chatCreated(Chat arg0, boolean arg1) {                    arg0.addMessageListener(new MessageListener() {                        @Override                        public void processMessage(Chat chat, Message message) {                            Log.i("收到消息", message.getFrom() + ":" + message.getBody());                        }                    });                }            });

发送消息
 newChat.sendMessage(message);
修改状态
            //设置登陆后的个人状态信息              Presence p = new Presence(Presence.Type.available);              p.setStatus("吃饭...");              connection.sendPacket(p);  
下线
connection.disconnect(); 
获取好友列表

        Collection<RosterEntry> rosters = connection.getRoster().getEntries();          Log.i("zyh","-------我的好友列表-------");          for(RosterEntry rosterEntry : rosters){              Log.i("zyh","name: "+rosterEntry.getName()+",jid: "+rosterEntry.getUser());          }  











0 0
原创粉丝点击