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

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


2.需要一个xmpp协议的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.         // Create a connection

  7.         ConnectionConfiguration connConfig =

  8.                 new ConnectionConfiguration(host, Integer.parseInt(port), service);

  9.         XMPPConnection connection = new XMPPConnection(connConfig);

  10.         

  11.         try {

  12.             Log.v("aaaaaaaaaaaaaaaaaaaa", "start");

  13.             connection.connect();

  14.             Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());

  15.         } catch (XMPPException ex) {

  16.             Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());

  17.             Log.e("XMPPClient", ex.toString());

  18.             //xmppClient.setConnection(null);

  19.         }

  20.         

  21.         

  22.         try {

  23.             connection.login(username, password);

  24.             Log.i("XMPPClient", "Logged in as " + connection.getUser());



  25.             // Set the status to available

  26.             Presence presence = new Presence(Presence.Type.available);

  27.             connection.sendPacket(presence);

  28.             String to = "推送目的地邮箱";

  29.             Message msg = new Message(to, Message.Type.chat);

  30.             msg.setSubject("VideoID");

  31.             msg.setBody("刘新云是sb"); // i is used to indicate this message as sending video ID

  32.             connection.sendPacket(msg);

  33.           

  34.         } catch (XMPPException ex) {

  35.             Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);

  36.             Log.e("XMPPClient", ex.toString());

  37.                 //xmppClient.setConnection(null);

  38.         }

复制代码
ps.记得配置权限:
不然请求会被拒绝
4.打开你的gtalk看看是不是有一条新的消息(其中你推送的邮箱最好是你加过的好友)

安卓Developer交流Q群欢迎您的加入273843464