如果人不在,程序突然出錯了誰來通知你?
如果人不在,想知道數(shù)據(jù)處理的進度怎么辦?
自己動手,打造GTalk小秘書
用法像log一樣簡單:
GTalk.sent("Hello Kitty");
OK,那就讓我們開始吧
枚舉,用來約定發(fā)送模式
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();
}
獲取發(fā)送對象地址的封裝類:
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 的結(jié)構(gòu)如下:
<?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;
}
// 發(fā)送消息
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) {
}
}
}
調(diào)用方法:
將GTalkRobotConst里的const值進行賦值
分別是:機器人的Google account名
機器人的Google account密碼
以及機器人發(fā)送消息對象列表的形式
如果形式為ALL,則消息會發(fā)送給機器人的所有好友
如果形式為PEOPLES_IN_LIST,則消息只發(fā)給在members.xml文件中注冊的人
最后,在需要log的時候調(diào)用
GTalk.sent("Hello Kitty");
消息就被發(fā)送到對應發(fā)送對象的GTalk上了
=。=
posted on 2011-07-18 10:17
Ying-er 閱讀(896)
評論(0) 編輯 收藏