Как перезапустить CountDownTimer при повторном входе в действие - PullRequest
1 голос
/ 16 марта 2012

В: Не знаю, как перезапустить CountDownTimer (CountDownTimer, который использует SharedPreferences для возврата из последних секунд, оставшихся после того, как пользователь покинул Activity), когда пользователь снова входит в мою 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 Ответ

1 голос
/ 16 марта 2012

Мое предложение:

@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
}
...