如果人不在,程序突然出错了谁来通知你?
如果人不在,想知道数据处理的进度怎么办?
自己动手,打造GTalk小秘书
用法像log一样简单:
GTalk.sent("Hello Kitty");
OK,那就让我们开始吧
枚举,用来约定发送模式
package com.yinger.util.gtalkRobot;
/**
* Representing GTalk sent modes
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/15 14:52:15
* @version 1.00
*/
public enum GTalkSentMode {
/**
* Indicating sent to peoples in list.
*/
PEOPLES_IN_LIST,
/**
* Indicating sent to All peoples
*/
ALL;
}
对机器人行为及信息进行注册:
package com.yinger.util.gtalkRobot;
/**
* Const Settings
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 8:53:07
* @version 1.00
* the jars this util needs:
* ---dom4j-1.6.1.jar
* ---smack.jar
* ---smackx.jar
* ---smackx-debug.jar
* ---smackx-jingle.jar
*/
public class GTalkRobotConst {
/**
* robot name
*/
public static String NAME = "yinger.android";
/**
* robot account password
*/
public static String PSWD = "xxxxxxxx";
/**
* PEOPLES_IN_LIST : peoples in members.xml
* ALL : all robot's friends
*/
public static String MODE = GTalkSentMode.PEOPLES_IN_LIST.toString();
}
获取发送对象地址的封装类:
package com.yinger.util.gtalkRobot;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
/**
* get members
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/15 16:25:43
* @version 1.00
*/
public class GetMembers {
public List<String> getXMLMembers() {
List<String> membersLs = new ArrayList<String>();
Document document;
Element root;
String xmlFile = "members.xml";
File file = new File(xmlFile);
try {
if (file.exists()) {
SAXReader reader = new SAXReader();
document = reader.read(file);
root = document.getRootElement();
} else {
document = DocumentHelper.createDocument();
root = document.addElement("members");
}
List infoNodes = root.elements("member");
for (Iterator it = infoNodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
String addr = elm.elementText("addr");
membersLs.add(addr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return membersLs;
}
public List<String> getAllMemebers(XMPPConnection connection) {
List<String> membersLs = new ArrayList<String>();
try {
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for(RosterEntry entry : entries){
String member = entry.getUser();
membersLs.add(member);
System.out.println("member:" + member);
}
} catch (Exception e) {
e.printStackTrace();
}
return membersLs;
}
}
其中,members.xml 的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<members>
<member>
<addr>linying0620@gmail.com</addr>
</member>
<member>
<addr>melody.crzaycoding@gmail.com</addr>
</member>
</members>
程序核心类,注意,是单实例的哦 =。=
package com.yinger.util.gtalkRobot;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
/**
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011-7-17 下午04:50:50
* @version 1.00
*/
public class SmackToGtalk {
private static SmackToGtalk instance;
private static XMPPConnection connection;
private static ArrayList<Chat> cookiesChatLs = new ArrayList<Chat>();
final private static String robotName = GTalkRobotConst.NAME;
final private static String robotPswd = GTalkRobotConst.PSWD;
public SmackToGtalk(String mode) {
getConnection();
createChat(mode);
}
public static SmackToGtalk getInstance(String mode) {
if (instance == null) {
instance = new SmackToGtalk(mode);
}
return instance;
}
/**
* connect to talk.google.com and login
*/
public XMPPConnection getConnection() {
ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
"talk.google.com", 5222, "gmail.com");
connectionConfig.setSASLAuthenticationEnabled(false);
if (connection == null) {
connection = new XMPPConnection(connectionConfig);
}
try {
connection.connect();
connection.login(robotName, robotPswd);
} catch (XMPPException e) {
e.printStackTrace();
}
return connection;
}
/**
* create chat with members
*/
public List<Chat> createChat(String mode) {
ChatManager chatmanager = connection.getChatManager();
ArrayList<String> memberLs = new ArrayList<String>();
if (mode.equals(GTalkSentMode.PEOPLES_IN_LIST.toString())) {
memberLs = (ArrayList<String>) (new GetMembers()).getXMLMembers();
} else if (mode.equals(GTalkSentMode.ALL.toString())) {
memberLs = (ArrayList<String>) (new GetMembers()).getAllMemebers(connection);
}
for (int i = 0; i < memberLs.size(); i++) {
String user = memberLs.get(i);
Chat cookiesChat = chatmanager.createChat(user,
new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.print(message.toXML());
}
});
cookiesChatLs.add(cookiesChat);
System.out.println(memberLs.get(i));
}
return cookiesChatLs;
}
// 发送消息
public void sendMessage(String message) {
try {
if (connection.isConnected() == false) {
connection.connect();
}
for (int i = 0; i < cookiesChatLs.size(); i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
connection.disconnect();
e.printStackTrace();
}
(cookiesChatLs.get(i)).sendMessage(message);
}
} catch (XMPPException e) {
e.printStackTrace();
}
}
// 在线时长 isOn :true 永久在线 如果为false 则需设定time时间长度
public void onLine(boolean isOn, int time) {
if (isOn) {
while (true) {
try {
Thread.sleep(1000 * 60 * 60 * 4);
} catch (InterruptedException e) {
connection.disconnect();
e.printStackTrace();
}
}
} else {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
connection.disconnect();
e.printStackTrace();
}
}
}
// 退出
public void loginOut() {
connection.disconnect();
}
}
对机器人的一个封装:
package com.yinger.util.gtalkRobot;
/**
* GTalk manager
*
* @author Ying-er
* @mail melody.crazycoding@gmail.com
* @time 2011/07/18 8:45:54
* @version 1.00
*/
public final class GTalk {
public static SmackToGtalk gTalk = SmackToGtalk
.getInstance(GTalkRobotConst.MODE);
public static void sent(String str) {
try {
gTalk.sendMessage(str);
} catch (Exception e) {
}
}
}
调用方法:
将GTalkRobotConst里的const值进行赋值
分别是:机器人的Google account名
机器人的Google account密码
以及机器人发送消息对象列表的形式
如果形式为ALL,则消息会发送给机器人的所有好友
如果形式为PEOPLES_IN_LIST,则消息只发给在members.xml文件中注册的人
最后,在需要log的时候调用
GTalk.sent("Hello Kitty");
消息就被发送到对应发送对象的GTalk上了
=。=
posted on 2011-07-18 10:17
Ying-er 阅读(892)
评论(0) 编辑 收藏