where should I edit this code, that i can stop the CountDown ?
How to delete the old one before I start the next new CountDown ?
public void MyCounter1(){
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
}.start();
}
here is code:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
};
timer.start();
}
// To stop & start new timer check not null of timer instance first then cancel existing timer & start new one
if(timer != null){
timer.cancel();
MyCounter1();
}
also if you want to cancel first instance and start new one you can add above lines in onFinish method which will be get called when timer finish his time.
check this:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
if(timer != null){
timer.cancel();
MyCounter1();
}
}
};
timer.start();
}
I did these 2 lines. I works at the time fine:
public void MyCounter1(){
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
if(my_condition=1){ //1- just make my_condition<>1 to stop the counter
txt_timer.setText("Left time : " + millisUntilFinished / 1000); }
}
public void onFinish() {
countDownTimer.cacel(); // 2- delete old one
txt_timer.setText("done");
}
}.start();
}
private int time = 10; //example 10 min.
CountDownTimer(60000 * time, 1000)
#Override
public void onFinish() {
txt_timer.setText("done");
cancel();
time = 0;
Related
I have the user inputting the time in the countdown timer.
Right now, its fixed to 10sec. How can i change this to user input ?
CountDownTimer counter = new CountDownTimer(10000,1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
Call this method where you take the input from the user:
public void startCountDown(long duration) {
CountDownTimer counter = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//millisUntilFinished=20000;
settime.setText("" + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
quescount++;
if (quescount%5==0){
round ++;
rndno.setText(String.valueOf(round));
// onBackPressed();
}
quescounter.setText("Out of "+String.valueOf(quescount));
// j = randomcount(getcount(),1);
++qcounter;
//j=qcounter;
if (qcounter<count)
setdata(String.valueOf(list.get(qcounter)));
else
{
Collections.shuffle(list);
qcounter=0;
setdata(String.valueOf(list.get(qcounter)));
}
}
};
}
You'll probably want to make your CountDownTimer variable global as well.
In my activity i want to check if countdowntimer is finished i'll stop the activity and pass to another activity?How can i do this?
I define countdowntimer like this
mcountdowntimer = new CountDownTimer(25000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i);
}
#Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
}
};
mcountdowntimer.start();
mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.rgb(64,91,164)));
}
Just simply add that logic in your onFinish() method:
#Override
public void onFinish() {
//here add the code for starting a new activity
}
I have to restart a CountDownTimer. I read a lot of question here but no one of the answer helped me.
When I use the following code
if(Const.counter != null){
Const.counter.cancel();
Const.counter = null;
}
Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000);
Const.counter.start();
I started a new counter but the old one also continues work. Please help me solve it.
You can realize it by cancelling and restarting. The following example should work.
CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
isCounterRunning = false;
}
};
boolean isCounterRunning = false;
private void yourOperation() {
if( !isCounterRunning ){
isCounterRunning = true;
mCountDownTimer.start();
}
else{
mCountDownTimer.cancel(); // cancel
mCountDownTimer.start(); // then restart
}
}
I did some different trick here. Hope this will help you.
if (myCountDownTimer != null) {
myCountDownTimer.cancel();
}
myCountDownTimer = new MyCountDownTimer(10000, 500);
myCountDownTimer.start();
coutdown timer for quiz
if(countDownTimer!=null)
{
countDownTimer.cancel();
countDownTimer.start();
}
else {
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long l) {
mtimer.setText("remaining time" + l / 1000);//mtime is a textview
}
public void onFinish() {//here mnext is the button from which we can get next question.
mnext.performClick();//this is used to perform clik automatically
}
}.start();
Just call again the start() method:
CountDownTimer cdt = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
this.start(); //start again the CountDownTimer
}
};
private fun startTimer() {
var timeInMilliSeconds = 11000L
val countDownTimer: CountDownTimer = object : CountDownTimer(timeInMilliSeconds, 1000) {
override fun onFinish() {
Timber.d("Times Up!")
setupResult("")
this.cancel()
timeInMilliSeconds = 11000L
this.start()
}
override fun onTick(p0: Long) {
val seconds = (p0 / 1000) % 60
Timber.d("Timer: $p0")
timer?.text = "$seconds"
}
}
countDownTimer.start()
}
I have a timer running for my IQ activity, and I want to Pause that timer when user click on Answer Button.
Bellow is my running timer and I have cancel timer when onlick but it's doesn't work for me:
timer = (TextView) findviewbyid(r.id.time)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
banana.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
CountDownTimer.cancel();
}
}
You can create a variable 'total' for total time and just set: total = millisUntilFinished in the onTick method. Check this answer: https://stackoverflow.com/a/6469166/2126403
Basically my aim is to have this timer reset whenever the user presses button b. I've tried a few methods such as if( i==true && bIsPressed()) but no luck, any ideas?
//2 buttons
Button =b;
TextView = time;
//countdown code
CountDownTimer Count = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
time.setText("Finished");
}
}; Count.start();
Haven't tested it, but I would do something along the lines of:
private void setupTimerResetButton()
{
mTimerResetButton.setOnClickListener(new OnClickListener(){
public void onClick(){
resetTimer();
}
});
}
private void resetTimer()
{
if(mTimer != null){
mTimer.cancel();
mTimer = null;
}
mTimer = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
mTimerTextView.setText(""+millisUntilFinished / 1000);
}
public void onFinish() {
mTimerTextView.setText("Finished");
}
};
mTimer.start();
}