How to loop through timer class for delayed time?
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask()
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++)
final int finalA = a;
handler.postDelayed(new Runnable()
@Override
public void run()
startTimer(30000);
, 30000); // delay until to finish first timer for 30 sec
java android for-loop countdowntimer
add a comment |
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask()
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++)
final int finalA = a;
handler.postDelayed(new Runnable()
@Override
public void run()
startTimer(30000);
, 30000); // delay until to finish first timer for 30 sec
java android for-loop countdowntimer
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
Nov 10 at 11:01
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
Nov 10 at 11:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask()
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++)
final int finalA = a;
handler.postDelayed(new Runnable()
@Override
public void run()
startTimer(30000);
, 30000); // delay until to finish first timer for 30 sec
java android for-loop countdowntimer
I want run timer for about 30000 ms and up to 8 or more times each so here is my loop but it runs all timers at once after 30000ms
public void repeatTimerTask()
repeat = 8; // need to run 30 sec timer for 8 times but one after one
startTimer(30000); // firsat timer for 30 sec
Handler handler = new Handler();
for (int a = 1; a<=repeat; a++)
final int finalA = a;
handler.postDelayed(new Runnable()
@Override
public void run()
startTimer(30000);
, 30000); // delay until to finish first timer for 30 sec
java android for-loop countdowntimer
java android for-loop countdowntimer
edited Nov 10 at 11:52
Kling Klang
32.2k156287
32.2k156287
asked Nov 10 at 10:47
Thippesh S
31
31
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
Nov 10 at 11:01
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
Nov 10 at 11:25
add a comment |
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a customRunnable
.
– Mark Keen
Nov 10 at 11:01
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and theHandler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.
– Mark Keen
Nov 10 at 11:25
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
Nov 10 at 11:01
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
Nov 10 at 11:01
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because the Runnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
Nov 10 at 11:25
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because the Runnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
Nov 10 at 11:25
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
To run a timer for n seconds you can use CountDownTimer
Declare two varibales globbaly. One for number of times you want to repeat. and one to keep the count of repetaion.
private int NUM_REPEAT = 4;
private int REPEAT_COUNT = 0;
Then call this method wherever you want. One thing to note if you want to run this loop 5 times you have to give number of repeation 4. Cause to satrt this function you have to call it so that will not come in count.
private void startTimer()
new CountDownTimer(3000, 1000)
int secondsLeft = 0;
public void onTick(long ms)
if (Math.round((float) ms / 1000.0f) != secondsLeft)
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("remaining time is "+secondsLeft);
;
public void onFinish()
Log.d(TAG, "timer finished "+REPEAT_COUNT);
if (REPEAT_COUNT <= NUM_REPEAT)
startTimer();
REPEAT_COUNT++;
.start();
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer()
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat)
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
startTimer();
, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
startTimerCount++;
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best justprivate int startTimerCount = 1, repeat = 8;
andstartTimerCount++;
did everything
– Thippesh S
Nov 12 at 13:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To run a timer for n seconds you can use CountDownTimer
Declare two varibales globbaly. One for number of times you want to repeat. and one to keep the count of repetaion.
private int NUM_REPEAT = 4;
private int REPEAT_COUNT = 0;
Then call this method wherever you want. One thing to note if you want to run this loop 5 times you have to give number of repeation 4. Cause to satrt this function you have to call it so that will not come in count.
private void startTimer()
new CountDownTimer(3000, 1000)
int secondsLeft = 0;
public void onTick(long ms)
if (Math.round((float) ms / 1000.0f) != secondsLeft)
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("remaining time is "+secondsLeft);
;
public void onFinish()
Log.d(TAG, "timer finished "+REPEAT_COUNT);
if (REPEAT_COUNT <= NUM_REPEAT)
startTimer();
REPEAT_COUNT++;
.start();
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
add a comment |
up vote
0
down vote
accepted
To run a timer for n seconds you can use CountDownTimer
Declare two varibales globbaly. One for number of times you want to repeat. and one to keep the count of repetaion.
private int NUM_REPEAT = 4;
private int REPEAT_COUNT = 0;
Then call this method wherever you want. One thing to note if you want to run this loop 5 times you have to give number of repeation 4. Cause to satrt this function you have to call it so that will not come in count.
private void startTimer()
new CountDownTimer(3000, 1000)
int secondsLeft = 0;
public void onTick(long ms)
if (Math.round((float) ms / 1000.0f) != secondsLeft)
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("remaining time is "+secondsLeft);
;
public void onFinish()
Log.d(TAG, "timer finished "+REPEAT_COUNT);
if (REPEAT_COUNT <= NUM_REPEAT)
startTimer();
REPEAT_COUNT++;
.start();
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To run a timer for n seconds you can use CountDownTimer
Declare two varibales globbaly. One for number of times you want to repeat. and one to keep the count of repetaion.
private int NUM_REPEAT = 4;
private int REPEAT_COUNT = 0;
Then call this method wherever you want. One thing to note if you want to run this loop 5 times you have to give number of repeation 4. Cause to satrt this function you have to call it so that will not come in count.
private void startTimer()
new CountDownTimer(3000, 1000)
int secondsLeft = 0;
public void onTick(long ms)
if (Math.round((float) ms / 1000.0f) != secondsLeft)
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("remaining time is "+secondsLeft);
;
public void onFinish()
Log.d(TAG, "timer finished "+REPEAT_COUNT);
if (REPEAT_COUNT <= NUM_REPEAT)
startTimer();
REPEAT_COUNT++;
.start();
To run a timer for n seconds you can use CountDownTimer
Declare two varibales globbaly. One for number of times you want to repeat. and one to keep the count of repetaion.
private int NUM_REPEAT = 4;
private int REPEAT_COUNT = 0;
Then call this method wherever you want. One thing to note if you want to run this loop 5 times you have to give number of repeation 4. Cause to satrt this function you have to call it so that will not come in count.
private void startTimer()
new CountDownTimer(3000, 1000)
int secondsLeft = 0;
public void onTick(long ms)
if (Math.round((float) ms / 1000.0f) != secondsLeft)
secondsLeft = Math.round((float) ms / 1000.0f);
// resend_timer is a textview
resend_timer.setText("remaining time is "+secondsLeft);
;
public void onFinish()
Log.d(TAG, "timer finished "+REPEAT_COUNT);
if (REPEAT_COUNT <= NUM_REPEAT)
startTimer();
REPEAT_COUNT++;
.start();
edited Nov 12 at 10:27
answered Nov 10 at 10:52
Satyajit Das
13619
13619
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
add a comment |
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
Thanks for the code dear @Satyajit Das, I want to repeat CountDownTimer for n number times as well as n number of seconds. and this code is just timer, thank you
– Thippesh S
Nov 11 at 11:36
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
@ThippeshS Updated the answer with loop. Let me know if you need anything else.
– Satyajit Das
Nov 12 at 10:27
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
Thanks for the update dear @Satyajit Das, and exactly I have implemented as per your updated answer with the help of Rahul Sonpaliya's answer, but without handler and your updated answer also fits well for my need, thank you so much.
– Thippesh S
Nov 12 at 13:12
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer()
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat)
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
startTimer();
, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
startTimerCount++;
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best justprivate int startTimerCount = 1, repeat = 8;
andstartTimerCount++;
did everything
– Thippesh S
Nov 12 at 13:16
add a comment |
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer()
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat)
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
startTimer();
, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
startTimerCount++;
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best justprivate int startTimerCount = 1, repeat = 8;
andstartTimerCount++;
did everything
– Thippesh S
Nov 12 at 13:16
add a comment |
up vote
0
down vote
up vote
0
down vote
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer()
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat)
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
startTimer();
, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
startTimerCount++;
Please try the below code, and call 'startTimer' method where you first want to start your timer :
private int startTimerCount = 1, repeat = 8;
private void startTimer()
// if startTimerCount is less than 8 than the handle will be created
if(startTimerCount <= repeat)
// this will create a handler which invokes startTimer method after 30 seconds
new Handler().postDelayed(new Runnable()
@Override
public void run()
startTimer();
, 30000);
// do what you want
Toast.makeText(this, "startTimer " + startTimerCount, Toast.LENGTH_SHORT).show();
startTimerCount++;
answered Nov 10 at 11:14
Rahul Sonpaliya
1365
1365
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best justprivate int startTimerCount = 1, repeat = 8;
andstartTimerCount++;
did everything
– Thippesh S
Nov 12 at 13:16
add a comment |
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best justprivate int startTimerCount = 1, repeat = 8;
andstartTimerCount++;
did everything
– Thippesh S
Nov 12 at 13:16
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
Thanks for the code dear @Rahul Sonpaliya, it helps but not reliable, I just want to repeat (loop) timer for n number times as well as n number of seconds. and I am trying out with this, will updates once I done, thank you
– Thippesh S
Nov 11 at 11:38
thanks once again @Rahul Sonpaliya, without handler it works best just
private int startTimerCount = 1, repeat = 8;
and startTimerCount++;
did everything– Thippesh S
Nov 12 at 13:16
thanks once again @Rahul Sonpaliya, without handler it works best just
private int startTimerCount = 1, repeat = 8;
and startTimerCount++;
did everything– Thippesh S
Nov 12 at 13:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238170%2fhow-to-loop-through-timer-class-for-delayed-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your loop does not block and completes all iterations immediately so all handler instances are created at once, thus seeming to run a once, you need to start a new timer task on completion of the previous - this logic should be in the task itself, or some way of signalling completion, possibly with a callback inside a custom
Runnable
.– Mark Keen
Nov 10 at 11:01
Thanks for the comment @Mark Keen, yes what you said above is true but starting new timer is really hectic, because if user wants timer to run for about 32 times (on undefined number times) what do? or how to code for it?
– Thippesh S
Nov 10 at 11:17
The below answer is one of many possible solutions, which addresses the issue of starting a new task every 30 seconds. However it has flaws, there is no way to cancel tasks, and the
Handler
instances can cause short term memory leaks because theRunnable
instances have an implicit reference, which is held until processed, of the outer class.– Mark Keen
Nov 10 at 11:25