Android- CountDownTimer won't stop - android

my CountDownTimer won't stop, i call the cancel() method but nothing happens, it continues to work. I hope you can help me, i've searched something around the web and all say that cancel() method works, so where have i made a mistake?
cd=new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
if(isNetworkAvailable() == true) {
countdown.dismiss();
chiudi=false;
timerCount.start();
cd.cancel();
}
countdown.setMessage("00:"+ (millisUntilFinished/1000));
System.out.println("ancora");
}
#Override
public void onFinish() {
countdown.dismiss();
finish();
}
}.start();

Related

CountDownTimer is not cancelled

I define the countdowntimer here.
Then, new countdowntimer is defined for this variable in an onclick method:
public void showQuestion(int questionNumber){
questionTimer = new CountDownTimer(10500,1000) {
#Override
public void onTick(long millisUntilFinished) {
remainingTime.setText(Long.toString(millisUntilFinished/1000));
}
#Override
public void onFinish() {
remainingTime.setText("0");
showCorrectAnswer();
}
}.start();
}
Then i cancel this timer on another button' onclick method;
public void selectOption(View view) {
questionTimer.cancel();
}
In this time, it is succesfully cancelled. Then i am doing the same again. showQuestion method is working in the same way.
public void showQuestion(int questionNumber){
questionTimer = new CountDownTimer(10500,1000) {
#Override
public void onTick(long millisUntilFinished) {
remainingTime.setText(Long.toString(millisUntilFinished/1000));
}
#Override
public void onFinish() {
remainingTime.setText("0");
showCorrectAnswer();
}
}.start();
}
It is started succesfully. When i want to cancel this timer on another button onclick method, it is not working. Not any error. I hope it could be clear. Thank you so much.
The issue with your code is the timer can be started multiple times (sometimes not intentional) before the first timer is finished or cancelled. In that case, when you try to cancel it, you can only cancel the first timer. To solve this issue, you only have to check whether the timer is running, if it is running, wait for it to finish or cancel.
Define a global parameter:
Boolean timerRunning = false;
public void showQuestion(int questionNumber){
if (timerRunning) return;
timerRunning = true;
questionTimer = new CountDownTimer(10500,1000) {
#Override
public void onTick(long millisUntilFinished) {
remainingTime.setText(Long.toString(millisUntilFinished/1000));
}
#Override
public void onFinish() {
remainingTime.setText("0");
showCorrectAnswer();
timerRunning = false;
}
}.start();
}
Your selection option method can be changed to below: check if the timer is null before cancelling it.
public void selectOption(View view) {
if (questionTimer != null) questionTimer.cancel();
timerRunning = false;
}
public void selectOption(View view) {
questionTimer.cancel();
questionTimer = null;
}

Pause and resume android countdown timer

I'm having android countdown timer which is starting automatically. I need to pause and resume it. I've seen other solutions but they didn't work. My code is:
class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
timerTimedOut = false;
}
#Override
public void onFinish() {
if(timerTimedOut){
doSTH();
} else {
doSTHElse();
}
this.start();
}
#Override
public void onTick(long millisUntilFinished)
{
timerShow.setText(Long.toString(millisUntilFinished / 1000));
}
public void stop(){
timerTimedOut = true;
this.cancel();
onFinish();
}
}
What should I do to pause and resume it?
I have been dealing with this and after trying many things, I found this solution. Here you can find a great alternative for the Android CountDownTimer class:
https://gist.github.com/bverc/1492672
You just have to create a new class named CountDownTimer2, and paste that code. Then, use it instead of the normal CountDownTimer class, for example:
CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//Whatever you want to do onTick
}
#Override
public void onFinish() {
Log.i(TAG, "Timer finished");
}
};
cdt.start();
}
An then you just need to pass
cdt.pause();
or
cdt.resume();
Hope it helps.

execute a function within a timer

I ve got this function here:
public void stoprecording()
{
this.recorder.stop();
this.recorder.reset();
this.recorder.release();
}
which stops the recoridng. This is within the class Audio. I also got this function here:
public void recordtimer(final int timer)
{
/*First Part with timer*/
Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
CountDownTimer countDowntimer = new CountDownTimer(timer, 100000)
{
public void onTick(long millisUntilFinished) {}
public void onFinish() {
this.stoprecording();
}
};countDowntimer.start();
}
});
thread.start();
this.stoprecording();
}
also within the class Audio. I canĀ“t execute this.stoprecording(); because the class CountDownTimer doesn t have this function. How can I execute this function?
Looking at your code it seems there is no need to call stoprecording() from this, since it does not belong to CountDownTimer class you have defined.
Just call it like
public void onFinish() {
stoprecording();
}
or if you want to definitely use this, follow the #gary111 suggestion thus doing
public void onFinish() {
YourClass.this.stoprecording();
}

CountDownTimer not accurate

I have an Android service running in the background.
I want to be notified after a specific period of time (22 seconds), so I wrote:
private CountDownTimer mCountDownTimer = new CountDownTimer(22*1000,22*1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {
doSomething();
}
};
I run this, and get the notification after 40 seconds, and even 50 seconds. Am I doing something wrong? How can this be done?
Actually you are putting interval time as wll 22000, what this you are doing wrong. Second parameter is the interval.So , Do this :
CountDownTimer alertTimer = new CountDownTimer(22*1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do here what you want
}
#Override
public void onFinish() {
}
}.start();

android CountDownTimer onStart

i want to save the time when countDownTimer begin working, is there any method like onStart()?
timeToDirectAnswer = new CountDownTimer(25000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
};
The timer starts ticking the moment start() is called on it. You can get the current time wherever start() is called.

Categories

Resources