2013-03-22 82 views
0

我正在做一个应用程序,将使用CountDownTimer 我想使用1倒计时2次倒计时。 如果时间结束,则立即开始新的时间,以此类推。CounDownTimer如何设置计数2次?

现在我有这样的事情:

cdt = new CounDownTimer(time,1000) { 
    public void onTick(…) { 
     //code 
    } 
    public void onFinish() { 
     //and here I'm thinking to add a new time, but it doesn't work 
    } 
}; 

如何做到这一点?或者也许还有其他更简单的选择来解决这个问题?

感谢您的帮助!

回答

0

您的课程中的某处newMyCountdownTimer(time,interval).start();

private class MyCountdownTimer extends CountDownTimer 
{ 


    public MyCountdownTimer(long millisInFuture, long countDownInterval) 
    { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() 
    { 
     if (somecondition is satisfied) // be carefull otherwise the countdown keeps running 
     { 
     // newTime and newInterval are variables in the outer class 
      new MyCountdownTimer(newTime, newInterval).start(); 
     } 

    } 

    @Override 
    public void onTick(long millisUntilFinished) 
    { 
     // TODO Auto-generated method stub 

    } 

} 
+0

以及如果我想更改时间(例如10000,而不是5000)?怎么做? – Ganjira 2013-03-22 23:33:15

+0

我编辑了我的答案。 – 2013-03-22 23:49:45