重新进入活动时如何重新启动CountDownTimer

时间:2012-03-16 16:25:30

标签: android countdowntimer

问:当用户再次输入我的Activity时,不知道如何重新启动CountDownTimer(一个CountDownTimer,它使用SharedPreferences从用户离开Activity后的最后几秒返回)。我不知道在哪里放置ct.start()以便不会出现NullPointerException错误。

答案/工作代码:(感谢Calvin)

   ct=new CountDownTimer(86400000, 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;                       
        seconds = timer2 / seconds_in_millies;

          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() 

     { 
         time.setText("Ready!!");
         time.setTypeface(tf);
     }

  }.start();


}   
    public void onResume() { 
        super.onResume();  
         prefs = PreferenceManager.getDefaultSharedPreferences(this);
        xtime = System.currentTimeMillis()-prefs.getLong("time",System.currentTimeMillis());      

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

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

           editor.commit(); 

        ct.start();
    }

    public void onStop() {
        super.onStop(); 

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

1 个答案:

答案 0 :(得分:1)

我的建议:

@Override
public void onResume() { // This will be trigger when your activity is created or come to front
    // Load preference
    // Start timer
}

@Override
public void onStop() { // This will be triggered when your activity goes behind or before your activity destroyed.
   // Cancel timer
   // Save preference
}