-1

我有一个休息计时器应用程序。它使用计时器类来计时,并在我的休息时间结束时通过短信通知我。一切正常,只是一旦 if 语句变为真,它就会每秒向我发送一条短信。所以我在一分钟内收到了大约 60 条消息,它会在运行时继续运行。我试过用很多不同的方式重写 if 语句,但它仍然做同样的事情。我知道没有 if 循环之类的东西,但这就是它正在做的事情。now 变量在公共类中声明的计时器循环之外。我在下面发布了大部分类代码。

     private static long now = System.currentTimeMillis();
       public BreakTimer() {
        this.setText(when());
    }
     public void actionPerformed(ActionEvent ae) {
     (some more code)            
        long currenttime = System.currentTimeMillis() - now;
        int breaknotify = 15 - SettingsIni.breaknotifytime();

                    if ((currenttime) /6000 == breaknotify){                                    
                            try {
                                TextMessage.main(null);
                            } catch (AddressException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (MessagingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }               
                    }       
        }
public void start() {
        BreakTimer.reset();
        timer.start();     
    }
    public static void main(String[] args) {
        running = true;
        BreakTimer jtl = new BreakTimer();
        jtl.start();
    }
}
4

2 回答 2

1

您正在进行整数除法,这将经常返回 true。我建议这样做:

if ( (currenttime)/ 6000.0 == breaknotify

此外,您需要终止计时器。

于 2012-12-19T20:40:46.883 回答
1

您需要在发送第一条短信时停止计时器。Timer 类为此提供了一个 stop() 方法。

   if ((currenttime) /6000 == breaknotify){                                    
          try {
              TextMessage.main(null);
              //stop the timer here
           } catch (AddressException e) {
于 2012-12-19T20:39:36.223 回答