How to countDownTimer.cancel(); in OnTick(); function - java

Hi i am trying to cancel a countdown timer in On-tick function, It is working fine in lollipop but not working in Kitkat and below. How cancel it from a OnTick() function.
private void startTimer(final int minuti) {
countDownTimer = new CountDownTimer(60 * minuti * 1000 + sec * 1000,
500) {
#Override
public void onTick(long leftTimeInMilliseconds) {
CountDownTimer.cancel();
}
#Override
public void onFinish() {
}
}.start();
}

Somehow if you call countdowntimer.cancel of a CountDownTimer object in its own onTick method it won't work!
The simplest way to solve your problem is defining another CountDownTimer, same as the main one, in order to check the condition to call countdowntimer.cancel. The point is that you are calling objects cancel method from outside of its own onTick method.
Check the example below:
CountDownTimer countdowntimer;
CountDownTimer assist;
long millisuntillfinish = 10000;
int interval = 100;
countdowntimer = new CountDownTimer(millisuntillfinish, interval) {
#Override
public void onTick(long millisUntilFinished) {
// if(true) countdowntimer.cancel(); (doesnt work)
}
#Override
public void onFinish() {
}
};
assist = new CountDownTimer(timertillfinish, timerinterval) {
#Override
public void onTick(long millisUntilFinished) {
if (true) {
countdowntimer.cancel();
}
}
#Override
public void onFinish() {
assist.cancel();
}
};

I know this question is more than 6 years old, but calling super.cancel(); within onTick() did the trick for me.
Leaving this here for the next unfortunate soul looking for an answer to this question.
ie.
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
if(someCondition)
super.cancel();
}
public void onFinish() {
//do something else
}
}.start();

new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
if (((millisUntilFinished/1000)%5)==0){ //every 5 sec
if(true)
{
this.cancel();
}
}
}
public void onFinish() {
//do somethhing
}
}.start();

I have been looking around and I have found this post where they call to the cancel() method in the onTick() using an alternative to the Android countDownTimer.
I had also this problem, and I created a global variable of a countDownTimer and I called to the cancel method in the activity when I needed, like myCountDownTimer.cancel().
private CountDownTimer mCountDownTimer;
private CountDownTimer createTimer(){
return new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//do what you want
}
public void onFinish() {
//do what you want
}
}.start();
}
In your activity call:
mCountDownTimer = this.createTimer();
And if you want to cancel it:
mCountDownTimer.cancel();

Related

Coding a time condition together with an action

When the app launch, I have a button (btn1) and a timer starting (long start = System.nanoTome();).
Now, two things can happen:
either the button is clicked at any time BEFORE the time limit of 1 minute (do some code_A then)
or the timer (long elapsedTime = System.nanoTome() - start;) reaches the time limit of 1 minute (do some code_B then)
I have tried lots of various combinations, but I stuck on the logic with regard to the time dimension and the if/while or whatever loop that would express/trigger BOTH the time condition OR the action condition (button btn1 pressed) in parallel.
Does anybody have an idea? That would be of great help.
You can use the CountDownTimer: https://developer.android.com/reference/android/os/CountDownTimer
CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
codeB();
}
}.start();
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countDownTimer.cancel();
codeA();
}
});
This will create a timer with the length of 10 seconds and the onTick method is called every 1 second.
new CountDownTimer(timerLength, tickInterval)
To further visualize your question in the comments, whether the timer stops or not:
In this screenshot, I've let the timer run out. The timer was set to 5 seconds and after 5 seconds onFinish() was called, as seen by the message.
In this screenshot, I've cancelled the timer through the onClickListener, as you can see there is no message in the center, thus onFinish() hasn't been called.
You can use CountDownTimer and handle the state there, then invoke the proper method depending on the state:
Kotlin version:
class MyCountdownTimer: CountDownTimer(60000, 1000) {
private var state: CountdownState = CountdownState.IDLE
override fun onFinish() {
state = CountdownState.FINISHED
}
override fun onTick(p0: Long) {
if(state == CountdownState.IDLE) {
state = CountdownState.COUNTING
}
}
fun getState(): CountdownState {
return state
}
}
enum class CountdownState {
IDLE, COUNTING, FINISHED
}
And in your code:
val countdownTimer = MyCountdownTimer()
btn1.setOnClickListener {
countdownTimer.start()
}
btn2.setOnClickListener {
if(countdownTimer.getState() == State.RUNNING) {
callMethodWhenRunning()
} else if (countdownTimer.getState == State.FINISHED) {
callMethodWhenFinished()
}
}
Java version:
import android.os.CountDownTimer;
public class MyCountdownTimer extends CountDownTimer {
private CountdownState state = CountdownState.IDLE;
public MyCountdownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long l) {
if(state == CountdownState.IDLE) {
state = CountdownState.COUNTING;
}
}
#Override
public void onFinish() {
state = CountdownState.FINISHED;
}
public CountdownState getState() {
return state;
}
}
enum CountdownState {
IDLE, COUNTING, FINISHED
}
and usage:
MyCountdownTimer timer = new MyCountdownTimer(60000, 1000);
btn1.setOnClickListener(view -> {
timer.start();
});
btn2.setOnClickListener(view -> {
if(timer.getState() == State.RUNNING) {
callMethodWhenRunning();
} else if (timer.getState == State.FINISHED) {
callMethodWhenFinished();
}
});

Set Visibility after countdown ends

Despite thorough search of other user's questions, I don't seem to understand how to do a countdown timer. All I wanna do is setting visibility to GONE after 30 seconds without touching the screen. So far I've done this:
public class StatusFragment extends Fragment {
CountDownTimer countDownTimer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
}
public void onFinish() {
//SET VISIBILITY TO GONE
}
}.start();
cpHover.setOnClickListener(new OnClickListener() {
//Should I countDownTimer.start()? It says there's an error
#Override
public void onClick(View view) {
if(textBox.getVisibility()==View.GONE){
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
else if(textBox.getVisibility()==View.VISIBLE){
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
});
The process should be quite easy. Setting in onCreate() a 30-second countdown that starts every time the user clicks cpHover. When they click it again, it should restart the countdown. There are two ways of hiding the UI: clicking on the screen when it's visible or not clicking at all in 30+ seconds.
Thanks in advance.
add this inside your onCreate()
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//set visibility as gone here
if(textBox.getVisibility()==View.VISIBLE){
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
}, 30000);
Its very Simple, just change your code as per below :
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View. VISIBLE);
}
public void onFinish() {
//SET VISIBILITY TO GONE
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
}.start();
you want to start CountDownTimer in Click event then add above code in the click event
Happy Coading....
Update your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpCountDown();
cpHover.setOnClickListener(new OnClickListener() {
//Should I countDownTimer.start()? It says there's an error
#Override
public void onClick(View view) {
if(countDownTimer != null) {
countDownTimer.cancel();
}
setUpCountDown();
}
);
}
private void setUpCountDown() {
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
public void onFinish() {
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}.start();
}

how does countimedown timer work likewise three times?

i have code block as below shown. for examaple Can i work this code three times?
when i used for loop or other loops. it isnt performed i wanted process.
new CountDownTimer(3000,1000) {
#Override
public void onTick(long l) {
gostertext.setText(""+l/1000);
durumgoster.setText("KOŞ..");
}
#Override
public void onFinish() {
gostertext.setText("KOŞU bitti YÜRÜ");
CountDownTimer counter=new CountDownTimer(3000,1000) {
#Override
public void onTick(long l) {
gostertext.setText(""+l/1000);
durumgoster.setText("YÜRÜ..");
}
#Override
public void onFinish() {
gostertext.setText("bitti");
}
}.start();
}
}.start();
when a counter ended, other counter will start that will perform three times.
i writed code as over shown that is worked but i want to perform three times.
Can we solve this problem?
why not make the loops contents into 1 void,
and call it three times?
You could do something like(untested):
int countCounter = 0;
CountDownTimer mCountDownTimer = new CountDownTimer() {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
if (countCounter < 3){
countCounter++;
// cancel the counter and restart it!
mCountDownTimer.cancel();
mCountDownTimer.start();
}
}
};

CountDownTimer cancel() not Working

I am new to Android Development and trying to make little Game.
CountDownTimer.cancel() is not working for me.
Any Idea?
Thank Your for your Answer!
CountDownTimer cdt = new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
maxTime = (int) (millisUntilFinished / 1000);
timer.setText(String.valueOf(maxTime));
}
public void onFinish() {
}
};
if (startTimer == true) {
cdt.start();
} else {
cdt.cancel();
}
I have to do an assumption right here because the code doesn't show much! apparently you are using the countDownTimer inside your onCreate as an inner class so that will trigger the timer when startTimer == true and it would create the object no matter what! I guess it would be better to create a global instance of CountDownTimer.
And write your code in this way:
if(startTimer == true) {
cdt = new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
maxTime = (int) (millisUntilFinished / 1000);
timer.setText(String.valueOf(maxTime));
}
public void onFinish() {
}
}.start(); //start the countdowntimer
}
else{
cdt.cancel();
}
Boolean Myboolean = true;
CountDownTimer MyCountDownTimer = new CountDownTimer(10000, 1000)
{
#Override
public void onFinish()
{
}
#Override
public void onTick(long millisUntilFinished)
{
Log.i("onTick" , String.valueOf((millisUntilFinished / 1000) % 60));
if(!Myboolean ) { cancel(); }
}
}.start();
i had this issue , the CountDownTimer will not cancle with (MyCountDownTimer.cancel();) , the best way is to use Boolean , set the Boolean to false to cancel the CountDownTimer. in any place you want (Myboolean =false;)

How to pause and resume countdown timer?

I have a countdown timer in my game activity and I need it paused when I get incoming call or something that lose the focus from my activity. I tried like this but it does not work (the onPause method works, it stops the timer, but it wont resume it):
private long total = 180000;
MyCount brojacVremena = new MyCount(total, 1000);
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
#Override
public void onTick(long millisUntilFinished) {
total = millisUntilFinished;
vreme.setText("" + millisUntilFinished / 1000);
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
MyCount brojacVremena = new MyCount(total, 1000);
brojacVremena.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
brojacVremena.cancel();
}
I tried even this in my onPause:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
MyCount brojacVremenaNew = new MyCount(total, 1000);
brojacVremenaNew.cancel();
}
I do not think that you need to extend it, unless really necessary. I second what codeMagic said - store time time left in a variable in onPause() and DO NOT forget to save it in onSaveInstanceState() or to SharedPreference.
When your activity resumes, either due to config change or whatever, you can recreate your timer:
new CountDownTimer(millisUntilFinished, tickTime) {
public void onTick(long millisUntilFinished) {
millisUntilFinished -= tickTime;
// do you wanna know for whom the bell tolls?
}
public void onFinish() {
// it tolls for thee
millisUntilFinished = 0;
}
}.start();
In onPause(), cancel this timer and in onResume() create a new instance of the timer based on how much time is left :)
EDIT:
Something like this:
#Override
public void onResume(){
super.onResume();
millisUntilFinished = getMillisFromSharedPreference(KEY);
if(millisUntilFinished != 0){
new CountDownTimer(millisUntilFinished, tickTime) {
public void onTick(long millisUntilFinished) {
millisUntilFinished -= tickTime;
// do you wanna know for whom the bell tolls?
}
public void onFinish() {
// it tolls for thee
millisUntilFinished = 0;
}
}.start();
}
}
#Override
public void onPause(){
super.onPause();
saveMillisToSharedPreference(KEY,millisUntilFinished);
}

Categories

Resources