安卓实现gtalk的xmpp简单通信

这里尝试用安卓尝试对电脑上的一个gtalk客户端进行推送消息。

项目源代码可去我的Q群共享下载(250395324)

1.首先要有两个google账号,这里我们假设账号一和账号二


2.需要一个xmpp协议(smack)的jar包,下载地址有很多

lz用的是http://beem-project.com/projects/beem/repository/changes/libs/asmack-android-7-beem.jar


3.配置java代码(其中gmail邮箱代表了你的gtalk的账号)

  1. String host = "talk.google.com";  
  2.     String port = "5222";  
  3.     String service = "google.com";  
  4.     String username = "你的google邮箱";  
  5.     String password = "邮箱密码";  
  6.   
  7.     // Create a connection  
  8.     ConnectionConfiguration connConfig =  
  9.             new ConnectionConfiguration(host, Integer.parseInt(port), service);  
  10.     XMPPConnection connection = new XMPPConnection(connConfig);  
  11.       
  12.     try {  
  13.         Log.v("aaaaaaaaaaaaaaaaaaaa", "start");  
  14.         connection.connect();  
  15.         Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());  
  16.     } catch (XMPPException ex) {  
  17.         Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());  
  18.         Log.e("XMPPClient", ex.toString());  
  19.         //xmppClient.setConnection(null);  
  20.     }  
  21.       
  22.       
  23.     try {  
  24.         connection.login(username, password);  
  25.         Log.i("XMPPClient", "Logged in as " + connection.getUser());  
  26.   
  27.         // Set the status to available  
  28.         Presence presence = new Presence(Presence.Type.available);  
  29.         connection.sendPacket(presence);  
  30.         String to = "推送目的地邮箱";  
  31.         Message msg = new Message(to, Message.Type.chat);   
  32.         msg.setSubject("VideoID");  
  33.         msg.setBody("刘新云是sb"); // i is used to indicate this message as sending video ID  
  34.         connection.sendPacket(msg);  
  35.         
  36.     } catch (XMPPException ex) {  
  37.         Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);  
  38.         Log.e("XMPPClient", ex.toString());  
  39.             //xmppClient.setConnection(null);  
  40.     }  


ps.记得配置权限: <uses-permission android:name="android.permission.INTERNET" />

不然请求会被拒绝



如果希望接收消息,记得新建一个方法然后再方法里面监听connection对象

  1.    public void setConnection  
  2.    (XMPPConnection  
  3.            connection) {  
  4.   
  5.     if (connection != null) {  
  6.         // Add a packet listener to get messages sent to us  
  7.         PacketFilter filter = new MessageTypeFilter(Message.Type.chat);  
  8.         connection.addPacketListener(new PacketListener() {  
  9.             public void processPacket(Packet packet) {  
  10.                 Message message = (Message) packet;  
  11.                 if (message.getBody() != null) {  
  12.                     String fromName = StringUtils.parseBareAddress(message.getFrom());  
  13.                     Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");  
  14.                     //messages.add(fromName + ":");  
  15.                     //messages.add(message.getBody());  
  16.                     // Add the incoming message to the list view  
  17.                     /*mHandler.post(new Runnable() { 
  18.                         public void run() { 
  19.                             setListAdapter(); 
  20.                         } 
  21.                     });*/  
  22.                 }  
  23.             }  
  24.   
  25.         }, filter);  
  26.     }  
  27. }  


4.5 然后在 connection.sendPacket(msg);

这句代码下面调用该方法,此时app将监听服务器那边的异动了

this.setConnection(connection);


4.打开你的gtalk看看是不是有一条新的消息(其中你推送的邮箱最好是你加过的好友)


如果希望自己配置服务器的可以使用fireopen这样的开源服务器框架就可自己制作简单即时通信软件了

posted @ 2014-07-22 16:49  阳光小屋  阅读(137)  评论(0编辑  收藏  举报