2016-07-07 125 views
0

我正在开发Android应用程序,我有一个要求:倒计时时间的Android

1)倒计时时间从'x' minutes'1' minute,然后一旦在1 minute,在60 sec, 30 sec条款,然后0计数。

我按照1分钟倒计时(60000)。

我的代码是:

public void countdowntimer(long timeinmillis, long countdowninterval){ 
     Log.d("hi","Iamhere 0" + timeinmillis/60000); 
     new CountDownTimer(timeinmillis, countdowninterval) { 

      public void onTick(long timeinmillis) { 


       //Countdown the time in terms of minutes 

       Log.d("hi","Iamhere 2" + timeinmillis); 
       tv.setText(String.valueOf((timeinmillis)/60000) + "min"); 
       rootView.invalidate(); 
       if(timeinmillis <= 60000 && timeinmillis > 30000){ 
        tv.setText(String.valueOf(60) + "sec"); 
        rootView.invalidate(); 
       }else if(timeinmillis < 30000){ 
        tv.setText(String.valueOf(30) + "sec"); 
        rootView.invalidate(); 
       } 
      } 

      public void onFinish() { 

      } 
     }.start(); 
    } 

日志是:

Iamhere0 4 

Iamhere1 3 

为什么我的第二个日志显示我实现60秒1分钟比第一日志以及如何较小,30秒倒计时,一旦它在1分钟?

的预期结果是:

4 min 

3 min 

2 min 

1 min -> 60sec 

30sec 

0 
+0

如果您包含预期的输出,您应该收到更高质量的答案 – Mfusiki

+0

仅仅因为onTick已经减去第一个区间值...对于该格式,只需使用检查剩余时间是否低于... – AxelH

+0

仍然不工作..请参阅编辑代码 – Mark023

回答

-1

使用AtomicInteger保持线程状态

在第一条件其显示时间在分钟

if (min.get() > 1) { 
         mCount.setText(Integer.toString(min.get())+" min"); 
         handler.postDelayed(this, 1000); 
         min.getAndDecrement(); 
        } 

AND

在最后一分钟其前进到第二状态中秒,这显示时间

else { 

        if(min.get()== 1) { 
         mCount.setText("60 Sec"); 
         handler.postDelayed(this, 500); 
        } 
        else if(min.get() == 0){ 
         mCount.setText("30 Sec"); 
         handler.postDelayed(this, 500); 
        } 
        else 
         mCount.setText("0 Sec"); 
         min.getAndDecrement(); 
       } 

使用...........

final TextView mCount = (TextView) findViewById(R.id.count); 
     final Handler handler = new Handler(); 
     final AtomicInteger min = new AtomicInteger(4); 
     final Runnable counter = new Runnable() { 
      @Override 
      public void run() { 

       if (min.get() > 1) { 
        mCount.setText(Integer.toString(min.get())+" min"); 
        handler.postDelayed(this, 1000); 
        min.getAndDecrement(); 
       } else { 

        if(min.get()== 1) { 
         mCount.setText("60 Sec"); 
         handler.postDelayed(this, 500); 
        } 
        else if(min.get() == 0){ 
         mCount.setText("30 Sec"); 
         handler.postDelayed(this, 500); 
        } 
        else 
         mCount.setText("0 Sec"); 
         min.getAndDecrement(); 
       } 

      } 
     }; 
     handler.postDelayed(counter, 0); 

喜欢编码.....

+0

谢谢你的代码...所以我的倒计时间隔是60000ms。那么我在哪里使用这个 – Mark023

+0

删除倒计时的所有代码,并使用此代码........ – sushildlh

+0

好吧,但在这段代码我不明白你指定分钟的开始时间。假设我需要从4分钟开始计数,这4分钟您指定的位置是 – Mark023

0

尝试这样的:

public class MyTimeCount extends CountDownTimer { 
private Button btn_code; 

public MyTimeCount(long millisInFuture, long countDownInterval,Button btn) { 
    super(millisInFuture, countDownInterval); 
    btn_code=btn; 
} 

@Override 
public void onFinish() { 
    btn_code.setText("Reacquire"); 
    btn_code.setEnabled(true); 
} 
@Override 
public void onTick(long millisUntilFinished){ 
    btn_code.setEnabled(false); 
    btn_code.setText(millisUntilFinished /1000+"s"); 

} 

} 
1

我花了一些时间来回答这个问题,这是我holydays ......但这里是一个其他如果这仍然是为任何人进行干预的解决方案。

我更喜欢使用System.currentTimeInMilli来管理定时器。当我开始时,我计算每次打勾结束的时间,然后计算剩余时间以显示。

这种设计可以防止任何增量/减量变量定时器的延迟。为了保持这个类可重用,我在每个tick上使用Observer模式(本例中为100ms),Observable实例将发送给每个观察者。

public class RunnableTimer extends Observable implements Runnable { 

    private final Handler handler; 
    // Number of milliseconds to run before the end 
    private Long delay; 
    // Date in millis until the end of the timer 
    private Long countDownEnd = null; 
    //The calculate remaining time 
    private long time; 

    public RunnableTimer(Observer o){ 
     this.handler = new Handler(); 
     addObserver(o); 
    } 

    /** 
    * Start the timer 
    * @param delay : the number of milliseconds to run 
    */ 
    public void start(Long delay){ 
     this.delay = delay; 

     countDownEnd = System.currentTimeMillis() + delay; 
     handler.postDelayed(this, 0); 
    } 

    @Override 
    public void run() { 
     time = countDownEnd - System.currentTimeMillis(); 

     if (time > 0) 
      handler.postDelayed(this, 100); //Recalculate every 100ms, to keep some precision... can be more or less. 
     else 
      time = 0; //to prevent to show negative values 

     sendUpdate(); 
    } 


    public String toString(){ 
     long t = time/1000; 
     Log.d("time", "" + t); 
     if(t >= 60){ 
      return String.format("%d min.", t/60); 
     } if t <= 0){ 
      return "End"; 
     } else { 
      return String.format("%d sec.", (t/30+1)*30); 
     } 
    } 

    /** 
    * Send the instance to each observer. 
    */ 
    private void sendUpdate(){ 
     setChanged(); 
     notifyObservers(toString()); 
    } 
} 

从这个,你有一个计时器,使用系统时间计算剩余时间和toString方法,创建愿望模板。

要使用它,只需使用您想要的Observer创建一个实例即可。

public class Test implements Observer { 

    private RunnableTimer timer; 

    public Test(){ 
     timer = new RunnableTimer(this); 
     timer.start(65000) //65sec 
    } 

    @Override 
    public void update(Observable observable, Object data) { 
     if(data == timer){ //If the update comes from the timer 
      Log.d("timer", timer.toString()); 
     } 
    } 
} 

我不得不清除我的班的一大部分,但这应该工作正常,因为这是更简单的版本。

编辑:在你的情况下,你的活动/片段应该实现观察者,并在更新中,我记录计时器,你应该把toString结果放到你的文本框中。