Is it possible change millisInFuture without recreate CountDownTimer? - android

Here my code that uses the CountDownTimer class:
private void initCountDownTimer() {
countDownTimer = new CountDownTimer(millisInFuture, COUNT_DOWN_INTERVALE_MS) {
#Override
public void onTick(long millisUntilFinished) {
getView().setCountDownTimerValue(millisUntilFinished);
}
#Override
public void onFinish() {
getView().setCountDownTimerValue(0);
}
};
}
It works fine.
But the question is: Is it possible change millisInFuture without recreate new object CountDownTimer?

No there is no way to change millisInFuture, without re-initializing the CountDownTimer using the constructor, since it is not an exposed variable. You can cancel() the timer and start a new one

Related

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();
}

How to dispose CountDownTimer

I have main class called "MainActivity" and I'm lanuching it few times in my App.
private static CountDownTimer timer;
private static final long startTime = 15 * 1000;
private static final long interval = 1 * 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new StageCountDownTimer(startTime, interval);
}
private class StageCountDownTimer extends CountDownTimer {
public StageCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//STARTTING NEW ACTIVITY
}
#Override
public void onTick(long millisUntilFinished) {
}
}
Sometimes user need to close this activity before count down ends, and return to the this activity again. And then new count down is launching but the old one execute the code in onFinish() when previous count down ends. Everything works great when I start this code once. How to cancel/dispose/destroy this timer after exiting from activity? I tried timer.cancel() and nothing happen.
EDIT
I think I solved my problem by setting CountDownTimer timer as a public and in other Activity i'm just using MainActivity.timer.cancel()
You can use:
if (isFinishing()) {
CountDownTimer.cancel();
}
so if the acitvity is finishing, the countdowntimer gets canceled. and next time you open the acitvity, new countdowntimer will be up.
In addition to timer.cancel() (which should work) you can do in your timer's onFinish():
#Override
public void onFinish() {
if(!MainActivity.this.isFinishing()) {
// Start new activity...
}
}
I think I solved my problem by setting CountDownTimer timer as a public and in other Activity i'm just using MainActivity.timer.cancel()

Android - CountDownTimer will not use a variable as the first parameter

I'm trying to pass the countdowntimer a variable and use that variable as the amount of milliseconds to count down. If I simply enter the value the countdown works properly, but if I pass it a long variable it just runs the onFinish function.
Here's the actual code:
public CountDownTimer countDown = new CountDownTimer(respawnTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timer =(Integer)(int) millisUntilFinished / 1000;
if(timer < 31)
timerText.setTextColor(Color.parseColor("#FF0000"));
timerText.setText(timer.toString());
}
#Override
public void onFinish() {
timerText.setTextColor(Color.parseColor("#00FF00"));
timerText.setText("UP");
}
};
At this point I have respawnTime set to equal 360000 hoping for a 360 second countdown, but like I said it just immediately runs the onFinish. Simply changing the first parameter to a literal instead of a variable fixes everything but I need to use a variable here. Thanks in advance for the help!
Change
#Override
public void onTick(long millisUntilFinished) {
to
#Override
public void onTick(long respawnTime) {
use the variable you are sending in the constructor in onTick().
Edit
Here is one of mine
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
duration = millisInFuture;
interval = countDownInterval;
start();
}
#Override
public void onFinish() {
secs = 10;
Intent intent = new Intent(CalibrationTimeoutScreen.this, CalibrationTakeTempScreen.class);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
CalibrationTimeoutScreen.this.finish();
}
#Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
You did not run start(). Simply add .start() after the last '}' or add a new line calling countDown.start().

CounDownTimer how to set it for counting 2 times?

I'm doing an app which will use CountDownTimer
I would like to use 1 countdowntimer for 2 count downs.
If the time finishes, then a new time starts immediately, and so on.
For now I have something like this:
cdt = new CounDownTimer(time,1000) {
public void onTick(…) {
//code
}
public void onFinish() {
//and here I'm thinking to add a new time, but it doesn't work
}
};
How to do that? Or maybe is there other easier option to solve that problem?
Thanks for help!
Somewhere in your class newMyCountdownTimer(time, interval).start();
private class MyCountdownTimer extends CountDownTimer
{
public MyCountdownTimer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish()
{
if (somecondition is satisfied) // be carefull otherwise the countdown keeps running
{
// newTime and newInterval are variables in the outer class
new MyCountdownTimer(newTime, newInterval).start();
}
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
}

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