0

在这里,我有这个 CountDownTimer,我想在无限循环中启动和重新启动,但我不知道为什么它不起作用:

 prefs = PreferenceManager.getDefaultSharedPreferences(this);

   xtime = System.currentTimeMillis()-prefs.getLong("time",System.currentTimeMillis());      

   timer=24000-(xtime+prefs.getLong("time2",0)); 

   final SharedPreferences.Editor editor = prefs.edit();
   editor.putLong("time2",xtime+prefs.getLong("time2",0));  

   editor.commit(); 

   new CountDownTimer(timer, 1000) { 

     public void onTick(long elapsed) {

        long timer2=elapsed;
        hours = timer2 / hours_in_millies;
        timer2 %= hours_in_millies;
        minutes = timer2 / minutes_in_millies;
        timer2 %= minutes_in_millies;
        Log.v(TAG, "seconds" + seconds);
        if(seconds<10)
            seconds=timer2/1000;   
        else 
        seconds = timer2 / seconds_in_millies;

        if(seconds<10)
        seconds=timer2/100;   

        if(seconds>1&&seconds<2)                
        seconds=0;

          if(hours>=10&&minutes>=10&&seconds>=10)
           time.setText(hours + ":" + minutes + ":" + seconds);
             else 
                 if (hours<10&&minutes>=10&&seconds>=10)
                 time.setText("0"+hours + ":" + minutes + ":" + seconds);            
                else 
                    if (hours<10&&minutes<10&&seconds>=10)
                    time.setText("0"+hours + ":0" + minutes + ":" + seconds);
                else 
                    if (hours>=10&&minutes<10&&seconds<10)
                    time.setText("0"+hours + ":0" + minutes + ":0" + seconds);
                else 
                    if(hours>=10&&minutes>=10&&seconds<10)
                         time.setText(hours + ":" + minutes + ":0" + seconds);
                else 
                        if (hours>=10&&minutes<10&&seconds<10)
                         time.setText(hours + ":0" + minutes + ":0" + seconds);
                        else 
                            if(hours>=10&&minutes<10&&seconds>=10)
                             time.setText(hours + ":0" + minutes+":" + seconds);
                            else if(hours<10&&minutes>=10 && seconds<10)
                                 time.setText("0"+hours + ":" + minutes +":0"+ seconds);


     }

     public void onFinish() 
     {  editor.clear();            
        this.start();
     }

  }.start();

     xtime=System.currentTimeMillis(); 
     SharedPreferences.Editor editor2 = prefs.edit();
     editor2.putLong("time",xtime);         
     editor2.commit(); 

}

SharedPreferences 帮助我使用我的算法从 Activity 结束的那一点恢复 CountDownTimer。问题是,即使我在我的 OnFinish() 中调用 this.start(),我在 this.start 的行上有这个 NullPointerException 错误。我会很感激一些帮助。谢谢!PS:当计时器低于 10 秒时,我也遇到问题,之后出现以下数字,但有一个奖励数字。(95,85,75...15)。如果在我的:

  if(seconds<10)
        seconds=timer2/1000;   
    else 
    seconds = timer2 / seconds_in_millies;

我会用 timer2/10000 更改 seconds=timer2/1000 它根本不显示数字并在 10 处停止。

4

2 回答 2

2

这为您提供了一个无限的倒数计时器.. 30 秒.. 您可以计算创建了多少次新计时器以获取总时间过去.. 像这样

MyCount counter;
counter=new MyCount(30000,1000);
  counter.start();

MyCount 类

public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
        counter= new MyCount(30000,1000);
     counter.start();
     n=n+1;
    }
    @Override
    public void onTick(long millisUntilFinished) {
        s1=millisUntilFinished;
        r1=(30000-s1)/1000;


    }
    }



   }

得到总时间过去......

int time=(n*30)+r1;<--- total time elapsed..

暂停计时器使用

counter.cancel();

恢复使用

counter= new MyCount(s1,1000);
     counter.start();
于 2012-03-16T10:33:58.323 回答
1

你可以这样做..

在你的 oncreate

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

 Date date = (Date)settings.getString("date",0);
 Calendar c = Calendar.getInstance(); 
        Date d=c.getTime();
long diffInMs = d.getTime() - date.getTime();
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMis);
if (diffInSec>86400)
{
 SharedPreferences myPrefs = this.getSharedPreferences("PREFS_NAME",0);
SharedPreferences.Editor ed = myPrefs.edit();
ed.putString("Date", String.valueOf(d));
ed.commit();
  //allow your activity to open
}
 else
{
 counter= new MyCount(diffInSec*1000,1000);
 counter.start();
 }

MyCount 类

 public class MyCount extends CountDownTimer{
 public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
    Intent k=new Intent(Myactivity.this,Myactivity.class);
                 startActivity(k);
}
@Override
public void onTick(long millisUntilFinished) {
    s1=millisUntilFinished;
     <---- display s1/1000 to show number of seconds left

}
}

}

希望这有效..

于 2012-03-16T12:50:38.817 回答