5

Please is it possible to publish a countdown in my gmail status? Like "01:44:15:23" and its decrements continually.

4

2 回答 2

4

找到一篇好文章分享

Google Talk 使用 XMPP,那么如果您可以使用连接XMPP client到您Google account的客户端,则可以使用客户端而不是 Google Talk。

整个机制太简单了(使用 SmackXMPP Library是因为它很简单而且对我很有帮助):

  1. 登录。
  2. 计算现在和目标日期之间的差异。
  3. 发送存在

登录

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 中看到更改,但请放心,它已更改。如果您想确保更改,请您的一位朋友告诉您您的状态)。

于 2013-02-24T10:55:13.747 回答
2

它非常简单,只需status-counter.jar这里下载并编写一个脚本文件

java -jar /root/status-counter.jar -status SF -username username@gmail.com -password XXXXXX -datetime 2013-03-21T16:00:00+02:00 -type hours -decimals 0

并编写一个 cron 来完成这项工作

*/5 * * * * /path/script.sh > /dev/null

这将每 5 分钟更新一次您的状态。更多细节可以在这里找到。

于 2013-02-26T06:17:33.830 回答