请问我的gmail状态是否可以发布倒计时? 像“01:44:15:23”一样,它的不断减少。
答案 0 :(得分:4)
找到一个好article to share:
Google Talk使用XMPP,如果您可以使用XMPP client
与Google account
进行关联,则可以使用客户端而非Google Talk。
整个机制太简单了(使用Smack XMPP Library
,因为它很简单并且很适合我):
<强>登录强>
import org.jivesoftware.smack.XMPPConnection;
public void connect() {
XMPPConnection connection = new XMPPConnection(server); //Server is gmail.com for Google Talk.
connection.connect();
connection.login(username, password); //Username and password.
}
计算现在与目标日期之间的差异
此过程使用Java Calendar和Date对象完成:
import java.util.Calendar;
import java.util.Date;
{
Calendar calendar1 = Calendar.getInstance();
Date d = new Date();
calendar1.setTime(d);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(endLine); //End line is the date we're counting to.
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffDays = diff / (24 * 60 * 60 * 1000);
diff = diff % (24 * 60 * 60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
diff = diff % (60 * 60 * 1000);
long diffMinutes = diff / (60 * 1000);
diff = diff % (60 * 1000);
}
此代码计算两个日期之间的差异,以天,小时和分钟为单位。
发送状态
在计算差异之后,我们所要做的就是发送存在:
import org.jivesoftware.smack.packet.Presence;
{
String remaining = Long.toString(diffDays) + " day(s), " + Long.toString(diffHours) + " hour(s), " + Long.toString(diffMinutes) + " minute(s) " + message; //Message is usually: Until "something".
Presence presence = new Presence(Presence.Type.available);
presence.setStatus(remaining);
presence.setPriority(24); //Highest priority in Google Talk
presence.setMode(presenceMode); //This is one of XMPP modes (Available, Chat, DND, Away, XA).
connection.sendPacket(presence);
}
在此之后,人们会看到您的新状态,而不是Google Talk中的状态。 (请注意,您将无法看到Google Talk中的更改,但请放心更改。如果您想确保更改,请让您的朋友告诉您其状态。)
答案 1 :(得分:2)