2016-01-27 7 views
1

我编辑的代码。这里的时间间隔是存储所需延迟和索引初始化为0的向量。我得到了我的错误。这就是我所拥有的一直在做,需要在Android应用程序中实现定时器(具有可变时间间隔的性质)

public class MainActivity{ 
private int index; 
     //Some code and initializations 

    public void startTimer{ 
    index=0; 
    private Timer timer=new Timer(); 
    private TimerTask timertask=new MyTimerTask(); 
    // timer.schedule(timertask,0,1000);   //This line was causing trouble 

    } 

    private class MyTimerTask extends TimerTask 
     { 
       public void run() {  

      handler.postDelayed(new Runnable() { 

     int limit = interval.size(); 

     @Override 
     public void run() { 
      if (index < limit) { 
       Integer secondDelay = interval.get(k); 
       Log.e(TAG, "index= " + index + " interval= " + secondDelay + " seconds"); 

       //Some code 

       long delay = secondDelay * 1000; 
       index++; 
       handler.postDelayed(this, delay); 
      } else { 
       handler.removeCallbacksAndMessages(null); //Cancelling the handler.postDelayed 
       Log.e(TAG, "Cancelling timer"); 
       timer.cancel(); 
      } 
     } 
    }, 0); 
    } 
} 
} 

日志输出是在每1秒之后。

注释行导致错误。应该是 timer.schedule(timertask,0);现在它的工作正常。

+0

等待你是一个有点困惑,指数和k不声明,你不需要removeCallBacks e .... timer.cancel!??!请从我的答案 – appersiano

+0

的代码开始,我发现了错误,并已纠正它。谢谢,你是一个很好的帮助 –

回答

0

算了Timer:它会更容易些使用睡眠这在一个循环:

class MyTimerRunnable implements Runnable { 
    @Override public void run() { 
    for (int i = 1; i < 5; ++i) { 
     try { 
     Thread.sleep(/* for however long */); 
     } catch (InterruptedException e) { 
     Thread.currentThread().interrupt(); 
     throw new RuntimeException(e); 
     } 
     System.out.println("Current:"+new Date()); 
    } 
    } 
} 

,然后就开始这个在自己的Thread(例如new Thread(new MyTimerRunnable()).start();),或将其提交到Executor

0

我使用Handler类(android.os包中的一个)创建了我自己的计时器类。这是一个基本的想法。

您的班级包含一个Handler类型的字段。你在构造函数中实例化它。您还应该有一个名为interval的字段。

在构造函数中,在创建处理程序对象之后,您可以在处理程序上调用postDelayed。该方法需要两个参数。第一个是Runnable,第二个是ms的延迟。

要编写可运行对象,请记住在完成所需操作后再次使用完全相同的参数调用处理程序的postDelay

对于第二个参数,只需传递字段interval

tl; dr:你看我在这里做什么?当定时器被构建时,你会发布一段时间后发生的事件。当事件发生时,做你喜欢的事情,并发布另一个事件,在同一时间后发生。

要更改间隔,只需添加一个setter即可设置interval对象。而下一次runnable运行时,它会发布一个事件在新的时间间隔后运行!

这里是我的定时器类的代码:

import android.os.Handler; 

public class Timer { 
    private Handler handler; 
    private boolean paused; 

    private int interval; 

    private Runnable task = new Runnable() { 
     @Override 
     public void run() { 
      if (!paused) { 
       runnable.run(); 
       Timer.this.handler.postDelayed (this, interval); 
      } 
     } 
    }; 

    private Runnable runnable; 

    public int getInterval() { 
     return interval; 
    } 

    public void setInterval(int interval) { 
     this.interval = interval; 
    } 

    public void startTimer() { 
     paused = false; 
     handler.postDelayed (task, interval); 
    } 

    public void stopTimer() { 
     paused = true; 
    } 

    public Timer (Runnable runnable, int interval, boolean started) { 
     handler = new Handler(); 
     this.runnable = runnable; 
     this.interval = interval; 
     if (started) 
      startTimer(); 
    } 
} 

编辑:回顾你的问题后,好像你是不是采用了android,但您标记问题的机器人。无论如何,如果你真的没有,只要使用javax.swing.Timer就好多了。在Google上查找文档!

+0

帖子的标题说“android”,所以它似乎是用户使用android,但他不知道Handler类的存在,无论如何伟大的类! – appersiano

1

我也有类似的问题,我有Handler

final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      int minute = 0; 
      @Override 
      public void run() { 
       { 
        Log.d("TAG", "next run after "+minute+" minutes"); 
        long delay = minute * 60000; 
        minute++; 
        handler.postDelayed(this,delay); 
       } 
      } 
     }, 0); 

解决,这是logcat

01-27 11:49:55.830 23761-23761/? D/TAG: next run after 0 minutes 
01-27 11:49:55.945 23761-23761/? D/TAG: next run after 1 minutes 
01-27 11:50:56.015 23761-23761/? D/TAG: next run after 2 minutes 
01-27 11:52:56.115 23761-23761/? D/TAG: next run after 3 minutes 
01-27 11:55:56.225 23761-23761/? D/TAG: next run after 4 minutes 
...and so on 

我的例子有分钟的时间间隔,但你可以很容易地适应秒。

希望它有帮助。


编辑 我修改根据您的意见,例如

final ArrayList<Integer> interval = new ArrayList<>(); 
interval.add(1); 
interval.add(5); 
interval.add(10); 
interval.add(3); 

final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    int index = 0; 

    @Override 
    public void run() { 
     { 

      if (index < interval.size()) { 
       Integer secondDelay = interval.get(index); 

       Log.d("TAG", "next run after " + secondDelay + " seconds"); 
       long delay = secondDelay * 1000; 
       index++; 
       handler.postDelayed(this, delay); 
      } else { 
       Log.d("TAG", "I finish"); 
      } 

     } 
    } 
}, 0); 

的logcat:

01-27 17:51:09.940 28113-28113/? D/TAG: next run after 1 seconds 
01-27 17:51:10.940 28113-28113/? D/TAG: next run after 5 seconds 
01-27 17:51:15.950 28113-28113/? D/TAG: next run after 10 seconds 
01-27 17:51:25.960 28113-28113/? D/TAG: next run after 3 seconds 
01-27 17:51:28.965 28113-28113/? D/TAG: I finish 
+0

所以你的意思是“延迟”在handler.postDelayed应该包含所需的可变时间间隔? –

+0

是的,它会自动增加匿名可运行的每个项目 – appersiano

+0

根据您的建议,我使用delay = interval.get(k)* 1000; handler.postDelayed(this,delay);其中间隔是由可变延迟组成的向量,k是索引,但是如果我使用它,则输出始终相差1秒,而不考虑间隔[k]处的值 –

相关问题