-2

I'm making a simple app where I start a countdown timer e.g. 25 to 0 and when the timer expires it shows a template.But when my mobile is locked timer will be paused and when i unlocked it will resume .what can i do for continues timer ??

public class MyActivity extends Activity {
TextView tvCountDown;
MyCount counter;
long countDownInterval = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_auth);
    tvCountDown = (TextView) findViewById(R.id.tvCountDown);
    counter = new MyCount(6000, countDownInterval);
        counter.start();
}
public class MyCount extends CountDownTimer 
{

    public MyCount(long millisInFuture, long countDownInterval) 
    {
        super(millisInFuture, countDownInterval);
    }
    public void onTick (long millisUntilFinished) 
    {
        Log.v("test","onTick Seconds: "+millisUntilFinished+" : "+(millisUntilFinished / 1000));
        tvCountDown.setText ( formatTime(millisUntilFinished));
        System.out.print("");
    }
    public void onFinish() {
        tvCountDown.setText("done!");
    }
    public String formatTime(long millis) 
    {
        String output = "00:00:00";
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 60;

        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);
        String hoursD = String.valueOf(hours); 

        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
        if (hours < 10)
            hoursD = "0" + hours;

        output = hoursD + " : " + minutesD + " : " + secondsD;
        return output;
    }
}

1 Answers1

0

Just call .cancel() method when your activity/fragment going to destroy.

If you want to pause/resume yout timer go for this answer;

Community
  • 1
  • 1
GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17
  • but when .cancel is called timer will not be resumed but i want to pause the timer when the activity is in background and timer will be resume when onResume is called – Er Mayank Singhal Jan 23 '16 at 12:17