I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pa
public static void setTimerMillis(Context context, long millis)
{ SharedPreferences sp =
context.getSharedPreferences(SessionManager.FILE_USER, Context.MODE_PRIVATE);
SharedPreferences.Editor spe = sp.edit(); spe.putLong(SessionManager.TIMER_MILLIS, millis); spe.apply(); }
void setExamTimer() {
setTimerColors();
final long maxTimeToShow = 60000; //testing
final long lastTimeinMillisLeftSaved = TestyBookHelper.getTimerMillis(context); //take saved value from sharedpref
final long intervalTime = 1000;
donut_exam_timer.setMax((int) maxTimeToShow);
CountDownTimer countDownTimer = new CountDownTimer(lastTimeinMillisLeftSaved, intervalTime) {
@Override
public void onTick(long millis) {
TestyBookHelper.setTimerMillis(context, millis);
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); System.out.println(hms);
donut_exam_timer.setText(hms);
donut_exam_timer.setProgress(millis);
}
@Override
public void onFinish() {
}
};
countDownTimer.start();
}
public static long getTimerMillis(Context context) {
SharedPreferences sp = context.getSharedPreferences(SessionManager.FILE_USER, Context.MODE_PRIVATE);
return sp.getLong(SessionManager.TIMER_MILLIS, 60000L);}
Well there are no API to pause or resume it. What you should do is cancel()
the timer and store the time remaining in a variable. When the resume button is hit again, restart the timer with the value from the variable.
You know Chronometers may be of interest to you.