Android中的倒数计时器不同步?

时间:2012-10-25 21:57:31

标签: java android calendar count

我正在尝试制作一个倒数计时器。我想出了这个代码,最初我觉得它很有用。然而,当我运行应用程序时,发生了两件我不知道如何修复的事情......

  1. 它是不同步的,虽然它是系统时间和日期,它说只剩下60天而不是61,当我知道它是61 !!
  2. 如果我关闭应用程序并返回它会重置计数器......
  3. 任何有关这方面的帮助都会受到赞赏,不仅仅是个人项目。

    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final TextView dateBx = (TextView) findViewById(R.id.textView1);
            final TextView mTextField = (TextView) findViewById(R.id.mTextField);
            final TextView minBx = (TextView) findViewById(R.id.minBx);
            final TextView hourBx = (TextView) findViewById(R.id.hourBx);
    
            //set todays date
            DateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
            Calendar cal1 = Calendar.getInstance();
            //target date
            DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
            Calendar cal2 = Calendar.getInstance();
            cal2.set(2012,11,25);
            //set maths
            long time1 = cal1.getTimeInMillis();
            long time2 = cal2.getTimeInMillis();
            //difference variable
            long diff = time2 - time1;
            //equations
            long diffSec = diff / 1000;
            long dMins = diff / (60 * 1000);
            long dHour = diff / (60 * 60 * 1000);
            long dDay = diff / (24 * 60 * 60 * 1000);
    
           new CountDownTimer(diff, 1000) 
           {
    
                 public void onTick(long millisUntilFinished) 
                 {
                     mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
                     dateBx.setText("Days remaining: " + millisUntilFinished / (24 * 60 * 60 * 1000));
                     minBx.setText("Minutes remaining: " + millisUntilFinished / (60 * 1000));
                     hourBx.setText("hours remaining: " + millisUntilFinished / (60 * 60 * 1000));
                 }
    
                 public void onFinish() {
                     mTextField.setText("done!");
                 }
              }.start();
    
    
    
    }
    

    我的代码是我学到的一些东西的组合,在java和android中,如果你想到一个更好的方法一起完成它然后请让我知道:)

    由于

2 个答案:

答案 0 :(得分:1)

以下是如何同步countDownTimer的示例。

(根据Android,“onTick()”不准确)

诀窍是只使用“onFinish()”并使用接口启动一个新的计时器。

制作一个扩展类的countDownTimer:

public class oneShotTimer extends CountDownTimer {
    TickListener invokeOnFinish;    // Interface

public oneShotTimer (Context parent, int tickTime, TickListener contextPointer) {
    super(tickTime, tickTime);          // DEBUG: (/60)
....
}

OnFinish():

 public void onFinish() {
     invokeOnFinish.restartTimer();    
 }
 public void onTick() {
     Log.e("Timer", "Not suppose to happen");
 }

定义界面:

public interface TickListener {
    public void restartTimer();
}

现在在父对象上执行以下操作:

1)在启动计时器之前,在MillinSecond中获取时间 - > “开始时间”;

2)为“TickListener :: restartTimer”创建方法(我写了psadu代码)

@Override
public void restartTimer() {
    // check if timer is done (make your own variable to monitor that)

    // get the currentTime in MilliSecond

    // calc TickTime , if we know that 30 tick are already counted 
    // there for ((30+1) x wantedTickTime) give us the target time for the next tick.
    // TickTime = (31 x wantedTickTime) - (currentTime - startTime)  
    // for example, we want 1sec ticks, after 30 ticks it finished at 30.2 sec
    // => (31 x 1) - (40.2 - 10)  = 0.8 is the time we need to re start timer.

    // re-run a new Timer with the new TickTime 
}

现在让我们看看Java垃圾收集将如何处理它。

答案 1 :(得分:0)

Android不是实时操作系统,因此您可以计算出您的计时器每1000毫秒运行一次。

最实用的方法是,对于每次调用,获取当前日期,获取目标日期,并重新计算剩余的天数/小时/分钟/秒(就像您在应用程序开始时所做的那样)。

这也可以在关闭/打开您的应用时解决您的问题。